2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
70 /******************************************************************************
71 * This project provides two demo applications. A simple blinky style project,
72 * and a more comprehensive test and demo application. The
73 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
74 * The simply blinky demo is implemented and described in main_blinky.c. The
75 * more comprehensive test and demo application is implemented and described in
78 * This file implements the code that is not demo specific, including the
79 * hardware setup and FreeRTOS hook functions.
81 *******************************************************************************
82 * NOTE: Windows will not be running the FreeRTOS demo threads continuously, so
83 * do not expect to get real time behaviour from the FreeRTOS Windows port, or
84 * this demo application. Also, the timing information in the FreeRTOS+Trace
85 * logs have no meaningful units. See the documentation page for the Windows
86 * port for further information:
87 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
89 * This demo was created using Windows XP on a dual core laptop, and has since
90 * been maintained using Windows 7 on a quad core laptop.
92 *******************************************************************************
95 /* Standard includes. */
100 /* FreeRTOS kernel includes. */
101 #include "FreeRTOS.h"
104 /* This project provides two demo applications. A simple blinky style demo
105 application, and a more comprehensive test and demo application. The
106 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
108 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is 1 then the blinky demo will be built.
109 The blinky demo is implemented and described in main_blinky.c.
111 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not 1 then the comprehensive test and
112 demo application will be built. The comprehensive test and demo application is
113 implemented and described in main_full.c. */
114 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 1
116 /* This demo uses heap_5.c, and these constants define the sizes of the regions
117 that make up the total heap. heap_5 is only used for test and example purposes
118 as this demo could easily create one large heap region instead of multiple
119 smaller heap regions - in which case heap_4.c would be the more appropriate
120 choice. See http://www.freertos.org/a00111.html for an explanation. */
121 #define mainREGION_1_SIZE 7001
122 #define mainREGION_2_SIZE 18105
123 #define mainREGION_3_SIZE 2807
125 /*-----------------------------------------------------------*/
128 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
129 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
131 extern void main_blinky( void );
132 extern void main_full( void );
135 * Only the comprehensive demo uses application hook (callback) functions. See
136 * http://www.freertos.org/a00016.html for more information.
138 void vFullDemoTickHookFunction( void );
139 void vFullDemoIdleFunction( void );
142 * This demo uses heap_5.c, so start by defining some heap regions. It is not
143 * necessary for this demo to use heap_5, as it could define one large heap
144 * region. Heap_5 is only used for test and example purposes. See
145 * http://www.freertos.org/a00111.html for an explanation.
147 static void prvInitialiseHeap( void );
150 * Prototypes for the standard FreeRTOS application hook (callback) functions
151 * implemented within this file. See http://www.freertos.org/a00016.html .
153 void vApplicationMallocFailedHook( void );
154 void vApplicationIdleHook( void );
155 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
156 void vApplicationTickHook( void );
157 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
158 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
161 * Writes trace data to a disk file when the trace recording is stopped.
162 * This function will simply overwrite any trace files that already exist.
164 static void prvSaveTraceFile( void );
166 /*-----------------------------------------------------------*/
168 /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
169 use a callback function to optionally provide the memory required by the idle
170 and timer tasks. This is the stack that will be used by the timer task. It is
171 declared here, as a global, so it can be checked by a test that is implemented
172 in a different file. */
173 StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
175 /* The user trace event posted to the trace recording on each tick interrupt.
176 Note: This project runs under Windows, and Windows will not be executing the
177 RTOS threads continuously. Therefore tick events will not appear with a regular
178 interval within the trace recording. */
179 traceLabel xTickTraceUserEvent;
180 static portBASE_TYPE xTraceRunning = pdTRUE;
182 /*-----------------------------------------------------------*/
186 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
187 is only used for test and example reasons. Heap_4 is more appropriate. See
188 http://www.freertos.org/a00111.html for an explanation. */
191 /* Initialise the trace recorder and create the label used to post user
192 events to the trace recording on each tick interrupt. Use of the trace
193 recorder is optional. See http://www.FreeRTOS.org/trace for more
195 vTraceInitTraceData();
196 xTickTraceUserEvent = xTraceOpenLabel( "tick" );
198 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
200 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
206 /* Start the trace recording - the recording is written to a file if
207 configASSERT() is called. */
208 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
209 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
218 /*-----------------------------------------------------------*/
220 void vApplicationMallocFailedHook( void )
222 /* vApplicationMallocFailedHook() will only be called if
223 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
224 function that will get called if a call to pvPortMalloc() fails.
225 pvPortMalloc() is called internally by the kernel whenever a task, queue,
226 timer or semaphore is created. It is also called by various parts of the
227 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
228 size of the heap available to pvPortMalloc() is defined by
229 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
230 API function can be used to query the size of free heap space that remains
231 (although it does not provide information on how the remaining heap might be
232 fragmented). See http://www.freertos.org/a00111.html for more
234 vAssertCalled( __LINE__, __FILE__ );
236 /*-----------------------------------------------------------*/
238 void vApplicationIdleHook( void )
240 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
241 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
242 task. It is essential that code added to this hook function never attempts
243 to block in any way (for example, call xQueueReceive() with a block time
244 specified, or call vTaskDelay()). If application tasks make use of the
245 vTaskDelete() API function to delete themselves then it is also important
246 that vApplicationIdleHook() is permitted to return to its calling function,
247 because it is the responsibility of the idle task to clean up memory
248 allocated by the kernel to any task that has since deleted itself. */
250 /* Uncomment the following code to allow the trace to be stopped with any
251 key press. The code is commented out by default as the kbhit() function
252 interferes with the run time behaviour. */
254 if( _kbhit() != pdFALSE )
256 if( xTraceRunning == pdTRUE )
260 xTraceRunning = pdFALSE;
265 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
267 /* Call the idle task processing used by the full demo. The simple
268 blinky demo does not use the idle task hook. */
269 vFullDemoIdleFunction();
273 /*-----------------------------------------------------------*/
275 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
280 /* Run time stack overflow checking is performed if
281 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
282 function is called if a stack overflow is detected. This function is
283 provided as an example only as stack overflow checking does not function
284 when running the FreeRTOS Windows port. */
285 vAssertCalled( __LINE__, __FILE__ );
287 /*-----------------------------------------------------------*/
289 void vApplicationTickHook( void )
291 /* This function will be called by each tick interrupt if
292 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
293 added here, but the tick hook is called from an interrupt context, so
294 code must not attempt to block, and only the interrupt safe FreeRTOS API
295 functions can be used (those that end in FromISR()). */
297 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
299 vFullDemoTickHookFunction();
301 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
303 /* Write a user event to the trace log. Note: This project runs under
304 Windows, and Windows will not be executing the RTOS threads continuously.
305 Therefore tick events will not appear with a regular interval within the the
307 vTraceUserEvent( xTickTraceUserEvent );
309 /*-----------------------------------------------------------*/
311 void vApplicationDaemonTaskStartupHook( void )
313 /* This function will be called once only, when the daemon task starts to
314 execute (sometimes called the timer task). This is useful if the
315 application includes initialisation code that would benefit from executing
316 after the scheduler has been started. */
318 /*-----------------------------------------------------------*/
320 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
322 static portBASE_TYPE xPrinted = pdFALSE;
323 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
325 /* Called if an assertion passed to configASSERT() fails. See
326 http://www.freertos.org/a00110.html#configASSERT for more information. */
328 /* Parameters are not used. */
332 printf( "ASSERT! Line %d, file %s, GetLastError() %d\r\n", ulLine, pcFileName, GetLastError() );
334 taskENTER_CRITICAL();
336 /* Stop the trace recording. */
337 if( xPrinted == pdFALSE )
340 if( xTraceRunning == pdTRUE )
347 /* You can step out of this function to debug the assertion by using
348 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
350 while( ulSetToNonZeroInDebuggerToContinue == 0 )
358 /*-----------------------------------------------------------*/
360 static void prvSaveTraceFile( void )
364 fopen_s( &pxOutputFile, "Trace.dump", "wb");
366 if( pxOutputFile != NULL )
368 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
369 fclose( pxOutputFile );
370 printf( "\r\nTrace output saved to Trace.dump\r\n" );
374 printf( "\r\nFailed to create trace dump file\r\n" );
377 /*-----------------------------------------------------------*/
379 static void prvInitialiseHeap( void )
381 /* The Windows demo could create one large heap region, in which case it would
382 be appropriate to use heap_4. However, purely for demonstration purposes,
383 heap_5 is used instead, so start by defining some heap regions. No
384 initialisation is required when any other heap implementation is used. See
385 http://www.freertos.org/a00111.html for more information.
387 The xHeapRegions structure requires the regions to be defined in start address
388 order, so this just creates one big array, then populates the structure with
389 offsets into the array - with gaps in between and messy alignment just for test
391 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
392 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
393 const HeapRegion_t xHeapRegions[] =
395 /* Start address with dummy offsets Size */
396 { ucHeap + 1, mainREGION_1_SIZE },
397 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
398 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
402 /* Sanity check that the sizes and offsets defined actually fit into the
404 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
406 /* Prevent compiler warnings when configASSERT() is not defined. */
407 ( void ) ulAdditionalOffset;
409 vPortDefineHeapRegions( xHeapRegions );
411 /*-----------------------------------------------------------*/
413 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
414 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
415 used by the Idle task. */
416 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
418 /* If the buffers to be provided to the Idle task are declared inside this
419 function then they must be declared static - otherwise they will be allocated on
420 the stack and so not exists after this function exits. */
421 static StaticTask_t xIdleTaskTCB;
422 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
424 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
425 state will be stored. */
426 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
428 /* Pass out the array that will be used as the Idle task's stack. */
429 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
431 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
432 Note that, as the array is necessarily of type StackType_t,
433 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
434 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
436 /*-----------------------------------------------------------*/
438 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
439 application must provide an implementation of vApplicationGetTimerTaskMemory()
440 to provide the memory that is used by the Timer service task. */
441 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
443 /* If the buffers to be provided to the Timer task are declared inside this
444 function then they must be declared static - otherwise they will be allocated on
445 the stack and so not exists after this function exits. */
446 static StaticTask_t xTimerTaskTCB;
448 /* Pass out a pointer to the StaticTask_t structure in which the Timer
449 task's state will be stored. */
450 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
452 /* Pass out the array that will be used as the Timer task's stack. */
453 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
455 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
456 Note that, as the array is necessarily of type StackType_t,
457 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
458 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;