2 FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
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
78 * This file implements the code that is not demo specific, including the
\r
79 * hardware setup and FreeRTOS hook functions.
\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
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
92 *******************************************************************************
\r
95 /* Standard includes. */
\r
100 /* FreeRTOS kernel includes. */
\r
101 #include "FreeRTOS.h"
\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
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
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
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 7001
\r
122 #define mainREGION_2_SIZE 18105
\r
123 #define mainREGION_3_SIZE 2807
\r
125 /*-----------------------------------------------------------*/
\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
131 extern void main_blinky( void );
\r
132 extern void main_full( void );
\r
135 * Only the comprehensive demo uses application hook (callback) functions. See
\r
136 * http://www.freertos.org/a00016.html for more information.
\r
138 void vFullDemoTickHookFunction( void );
\r
139 void vFullDemoIdleFunction( void );
\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
147 static void prvInitialiseHeap( void );
\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
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 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
\r
158 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
\r
161 * Writes trace data to a disk file when the trace recording is stopped.
\r
162 * This function will simply overwrite any trace files that already exist.
\r
164 static void prvSaveTraceFile( void );
\r
166 /*-----------------------------------------------------------*/
\r
168 /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
\r
169 use a callback function to optionally provide the memory required by the idle
\r
170 and timer tasks. This is the stack that will be used by the timer task. It is
\r
171 declared here, as a global, so it can be checked by a test that is implemented
\r
172 in a different file. */
\r
173 StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
\r
175 /* The user trace event posted to the trace recording on each tick interrupt.
\r
176 Note: This project runs under Windows, and Windows will not be executing the
\r
177 RTOS threads continuously. Therefore tick events will not appear with a regular
\r
178 interval within the trace recording. */
\r
179 traceLabel xTickTraceUserEvent;
\r
180 static portBASE_TYPE xTraceRunning = pdTRUE;
\r
182 /*-----------------------------------------------------------*/
\r
186 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
\r
187 is only used for test and example reasons. Heap_4 is more appropriate. See
\r
188 http://www.freertos.org/a00111.html for an explanation. */
\r
189 prvInitialiseHeap();
\r
191 /* Initialise the trace recorder and create the label used to post user
\r
192 events to the trace recording on each tick interrupt. Use of the trace
\r
193 recorder is optional. See http://www.FreeRTOS.org/trace for more
\r
195 vTraceInitTraceData();
\r
196 xTickTraceUserEvent = xTraceOpenLabel( "tick" );
\r
198 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
200 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
206 /* Start the trace recording - the recording is written to a file if
\r
207 configASSERT() is called. */
\r
208 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
\r
209 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
\r
218 /*-----------------------------------------------------------*/
\r
220 void vApplicationMallocFailedHook( void )
\r
222 /* vApplicationMallocFailedHook() will only be called if
\r
223 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
224 function that will get called if a call to pvPortMalloc() fails.
\r
225 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
226 timer or semaphore is created. It is also called by various parts of the
\r
227 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
\r
228 size of the heap available to pvPortMalloc() is defined by
\r
229 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
230 API function can be used to query the size of free heap space that remains
\r
231 (although it does not provide information on how the remaining heap might be
\r
232 fragmented). See http://www.freertos.org/a00111.html for more
\r
234 vAssertCalled( __LINE__, __FILE__ );
\r
236 /*-----------------------------------------------------------*/
\r
238 void vApplicationIdleHook( void )
\r
240 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
241 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
242 task. It is essential that code added to this hook function never attempts
\r
243 to block in any way (for example, call xQueueReceive() with a block time
\r
244 specified, or call vTaskDelay()). If application tasks make use of the
\r
245 vTaskDelete() API function to delete themselves then it is also important
\r
246 that vApplicationIdleHook() is permitted to return to its calling function,
\r
247 because it is the responsibility of the idle task to clean up memory
\r
248 allocated by the kernel to any task that has since deleted itself. */
\r
250 /* Uncomment the following code to allow the trace to be stopped with any
\r
251 key press. The code is commented out by default as the kbhit() function
\r
252 interferes with the run time behaviour. */
\r
254 if( _kbhit() != pdFALSE )
\r
256 if( xTraceRunning == pdTRUE )
\r
259 prvSaveTraceFile();
\r
260 xTraceRunning = pdFALSE;
\r
265 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
267 /* Call the idle task processing used by the full demo. The simple
\r
268 blinky demo does not use the idle task hook. */
\r
269 vFullDemoIdleFunction();
\r
273 /*-----------------------------------------------------------*/
\r
275 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
277 ( void ) pcTaskName;
\r
280 /* Run time stack overflow checking is performed if
\r
281 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
282 function is called if a stack overflow is detected. This function is
\r
283 provided as an example only as stack overflow checking does not function
\r
284 when running the FreeRTOS Windows port. */
\r
285 vAssertCalled( __LINE__, __FILE__ );
\r
287 /*-----------------------------------------------------------*/
\r
289 void vApplicationTickHook( void )
\r
291 /* This function will be called by each tick interrupt if
\r
292 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
293 added here, but the tick hook is called from an interrupt context, so
\r
294 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
295 functions can be used (those that end in FromISR()). */
\r
297 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
299 vFullDemoTickHookFunction();
\r
301 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
303 /* Write a user event to the trace log. Note: This project runs under
\r
304 Windows, and Windows will not be executing the RTOS threads continuously.
\r
305 Therefore tick events will not appear with a regular interval within the the
\r
306 trace recording. */
\r
307 vTraceUserEvent( xTickTraceUserEvent );
\r
309 /*-----------------------------------------------------------*/
\r
311 void vApplicationDaemonTaskStartupHook( void )
\r
313 /* This function will be called once only, when the daemon task starts to
\r
314 execute (sometimes called the timer task). This is useful if the
\r
315 application includes initialisation code that would benefit from executing
\r
316 after the scheduler has been started. */
\r
318 /*-----------------------------------------------------------*/
\r
320 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
322 static portBASE_TYPE xPrinted = pdFALSE;
\r
323 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
325 /* Called if an assertion passed to configASSERT() fails. See
\r
326 http://www.freertos.org/a00110.html#configASSERT for more information. */
\r
328 /* Parameters are not used. */
\r
330 ( void ) pcFileName;
\r
332 printf( "ASSERT! Line %d, file %s, GetLastError() %d\r\n", ulLine, pcFileName, GetLastError() );
\r
334 taskENTER_CRITICAL();
\r
336 /* Stop the trace recording. */
\r
337 if( xPrinted == pdFALSE )
\r
340 if( xTraceRunning == pdTRUE )
\r
343 prvSaveTraceFile();
\r
347 /* You can step out of this function to debug the assertion by using
\r
348 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
350 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
356 taskEXIT_CRITICAL();
\r
358 /*-----------------------------------------------------------*/
\r
360 static void prvSaveTraceFile( void )
\r
362 FILE* pxOutputFile;
\r
364 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
366 if( pxOutputFile != NULL )
\r
368 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
369 fclose( pxOutputFile );
\r
370 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
374 printf( "\r\nFailed to create trace dump file\r\n" );
\r
377 /*-----------------------------------------------------------*/
\r
379 static void prvInitialiseHeap( void )
\r
381 /* The Windows demo could create one large heap region, in which case it would
\r
382 be appropriate to use heap_4. However, purely for demonstration purposes,
\r
383 heap_5 is used instead, so start by defining some heap regions. No
\r
384 initialisation is required when any other heap implementation is used. See
\r
385 http://www.freertos.org/a00111.html for more information.
\r
387 The xHeapRegions structure requires the regions to be defined in start address
\r
388 order, so this just creates one big array, then populates the structure with
\r
389 offsets into the array - with gaps in between and messy alignment just for test
\r
391 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
392 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
393 const HeapRegion_t xHeapRegions[] =
\r
395 /* Start address with dummy offsets Size */
\r
396 { ucHeap + 1, mainREGION_1_SIZE },
\r
397 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
398 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
402 /* Sanity check that the sizes and offsets defined actually fit into the
\r
404 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
406 /* Prevent compiler warnings when configASSERT() is not defined. */
\r
407 ( void ) ulAdditionalOffset;
\r
409 vPortDefineHeapRegions( xHeapRegions );
\r
411 /*-----------------------------------------------------------*/
\r
413 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
\r
414 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
\r
415 used by the Idle task. */
\r
416 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
\r
418 /* If the buffers to be provided to the Idle task are declared inside this
\r
419 function then they must be declared static - otherwise they will be allocated on
\r
420 the stack and so not exists after this function exits. */
\r
421 static StaticTask_t xIdleTaskTCB;
\r
422 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
\r
424 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
\r
425 state will be stored. */
\r
426 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
\r
428 /* Pass out the array that will be used as the Idle task's stack. */
\r
429 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
\r
431 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
\r
432 Note that, as the array is necessarily of type StackType_t,
\r
433 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
434 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
\r
436 /*-----------------------------------------------------------*/
\r
438 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
\r
439 application must provide an implementation of vApplicationGetTimerTaskMemory()
\r
440 to provide the memory that is used by the Timer service task. */
\r
441 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
\r
443 /* If the buffers to be provided to the Timer task are declared inside this
\r
444 function then they must be declared static - otherwise they will be allocated on
\r
445 the stack and so not exists after this function exits. */
\r
446 static StaticTask_t xTimerTaskTCB;
\r
448 /* Pass out a pointer to the StaticTask_t structure in which the Timer
\r
449 task's state will be stored. */
\r
450 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
\r
452 /* Pass out the array that will be used as the Timer task's stack. */
\r
453 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
\r
455 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
\r
456 Note that, as the array is necessarily of type StackType_t,
\r
457 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
458 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
\r