2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
96 /******************************************************************************
\r
97 * This project provides two demo applications. A simple blinky style project,
\r
98 * and a more comprehensive test and demo application. The
\r
99 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
\r
100 * The simply blinky demo is implemented and described in main_blinky.c. The
\r
101 * more comprehensive test and demo application is implemented and described in
\r
104 * This file implements the code that is not demo specific, including the
\r
105 * hardware setup and FreeRTOS hook functions.
\r
107 *******************************************************************************
\r
108 * -NOTE- The Win32 port is a simulation (or is that emulation?) only! Do not
\r
109 * expect to get real time behaviour from the Win32 port or this demo
\r
110 * application. It is provided as a convenient development and demonstration
\r
111 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
113 * Windows will not be running the FreeRTOS simulator threads continuously, so
\r
114 * the timing information in the FreeRTOS+Trace logs have no meaningful units.
\r
115 * See the documentation page for the Windows simulator for an explanation of
\r
117 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
\r
118 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
119 *******************************************************************************
\r
123 /* Standard includes. */
\r
125 #include <stdlib.h>
\r
128 /* FreeRTOS kernel includes. */
\r
129 #include "FreeRTOS.h"
\r
132 /* This project provides two demo applications. A simple blinky style project,
\r
133 and a more comprehensive test and demo application. The
\r
134 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
\r
135 The simply blinky demo is implemented and described in main_blinky.c. The more
\r
136 comprehensive test and demo application is implemented and described in
\r
138 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 0
\r
140 /* This demo uses heap_5.c, and these constants define the sizes of the regions
\r
141 that make up the total heap. This is only done to provide an example of heap_5
\r
142 being used as this demo could easily create one large heap region instead of
\r
143 multiple smaller heap regions - in which case heap_4.c would be the more
\r
144 appropriate choice. */
\r
145 #define mainREGION_1_SIZE 3001
\r
146 #define mainREGION_2_SIZE 18105
\r
147 #define mainREGION_3_SIZE 1107
\r
150 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
151 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
153 extern void main_blinky( void );
\r
154 extern void main_full( void );
\r
157 * Some of the RTOS hook (callback) functions only need special processing when
\r
158 * the full demo is being used. The simply blinky demo has no special
\r
159 * requirements, so these functions are called from the hook functions defined
\r
160 * in this file, but are defined in main_full.c.
\r
162 void vFullDemoTickHookFunction( void );
\r
163 void vFullDemoIdleFunction( void );
\r
166 * This demo uses heap_5.c, so start by defining some heap regions. This is
\r
167 * only done to provide an example as this demo could easily create one large
\r
168 * heap region instead of multiple smaller heap regions - in which case heap_4.c
\r
169 * would be the more appropriate choice. No initialisation is required when
\r
170 * heap_4.c is used.
\r
172 static void prvInitialiseHeap( void );
\r
175 * Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
176 * within this file.
\r
178 void vApplicationMallocFailedHook( void );
\r
179 void vApplicationIdleHook( void );
\r
180 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
181 void vApplicationTickHook( void );
\r
184 * Writes trace data to a disk file when the trace recording is stopped.
\r
185 * This function will simply overwrite any trace files that already exist.
\r
187 static void prvSaveTraceFile( void );
\r
189 /* The user trace event posted to the trace recording on each tick interrupt.
\r
190 Note tick events will not appear in the trace recording with regular period
\r
191 because this project runs in a Windows simulator, and does not therefore
\r
192 exhibit deterministic behaviour. */
\r
193 traceLabel xTickTraceUserEvent;
\r
194 static portBASE_TYPE xTraceRunning = pdTRUE;
\r
196 /*-----------------------------------------------------------*/
\r
200 /* This demo uses heap_5.c, so start by defining some heap regions. This
\r
201 is only done to provide an example as this demo could easily create one
\r
202 large heap region instead of multiple smaller heap regions - in which case
\r
203 heap_4.c would be the more appropriate choice. No initialisation is
\r
204 required when heap_4.c is used. */
\r
205 prvInitialiseHeap();
\r
207 /* Initialise the trace recorder and create the label used to post user
\r
208 events to the trace recording on each tick interrupt. */
\r
209 vTraceInitTraceData();
\r
210 xTickTraceUserEvent = xTraceOpenLabel( "tick" );
\r
212 /* Start the trace recording - the recording is written to a file if
\r
213 configASSERT() is called. */
\r
214 printf( "\r\nTrace started. Hit a key to dump trace file to disk (does not work from Eclipse console).\r\n" );
\r
218 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
220 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
232 /*-----------------------------------------------------------*/
\r
234 void vApplicationMallocFailedHook( void )
\r
236 /* vApplicationMallocFailedHook() will only be called if
\r
237 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
238 function that will get called if a call to pvPortMalloc() fails.
\r
239 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
240 timer or semaphore is created. It is also called by various parts of the
\r
241 demo application. If heap_1.c or heap_2.c are used, then the size of the
\r
242 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
243 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
244 to query the size of free heap space that remains (although it does not
\r
245 provide information on how the remaining heap might be fragmented). */
\r
246 vAssertCalled( __LINE__, __FILE__ );
\r
248 /*-----------------------------------------------------------*/
\r
250 void vApplicationIdleHook( void )
\r
252 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
253 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
254 task. It is essential that code added to this hook function never attempts
\r
255 to block in any way (for example, call xQueueReceive() with a block time
\r
256 specified, or call vTaskDelay()). If the application makes use of the
\r
257 vTaskDelete() API function (as this demo application does) then it is also
\r
258 important that vApplicationIdleHook() is permitted to return to its calling
\r
259 function, because it is the responsibility of the idle task to clean up
\r
260 memory allocated by the kernel to any task that has since been deleted. */
\r
262 /* Uncomment the following code to allow the trace to be stopped with any
\r
263 key press. The code is commented out by default as the kbhit() function
\r
264 interferes with the run time behaviour. */
\r
266 if( _kbhit() != pdFALSE )
\r
268 if( xTraceRunning == pdTRUE )
\r
271 prvSaveTraceFile();
\r
272 xTraceRunning = pdFALSE;
\r
277 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
279 /* Call the idle task processing used by the full demo. The simple
\r
280 blinky demo does not use the idle task hook. */
\r
281 vFullDemoIdleFunction();
\r
285 /*-----------------------------------------------------------*/
\r
287 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
289 ( void ) pcTaskName;
\r
292 /* Run time stack overflow checking is performed if
\r
293 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
294 function is called if a stack overflow is detected. */
\r
295 vAssertCalled( __LINE__, __FILE__ );
\r
297 /*-----------------------------------------------------------*/
\r
299 void vApplicationTickHook( void )
\r
301 /* This function will be called by each tick interrupt if
\r
302 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
303 added here, but the tick hook is called from an interrupt context, so
\r
304 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
305 functions can be used (those that end in FromISR()). */
\r
307 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
309 vFullDemoTickHookFunction();
\r
311 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
313 /* Write a user event to the trace log.
\r
314 Note tick events will not appear in the trace recording with regular period
\r
315 because this project runs in a Windows simulator, and does not therefore
\r
316 exhibit deterministic behaviour. Windows will run the simulator in
\r
318 vTraceUserEvent( xTickTraceUserEvent );
\r
320 /*-----------------------------------------------------------*/
\r
322 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
324 static portBASE_TYPE xPrinted = pdFALSE;
\r
325 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
327 /* Parameters are not used. */
\r
329 ( void ) pcFileName;
\r
331 taskENTER_CRITICAL();
\r
333 /* Stop the trace recording. */
\r
334 if( xPrinted == pdFALSE )
\r
337 if( xTraceRunning == pdTRUE )
\r
340 prvSaveTraceFile();
\r
344 /* You can step out of this function to debug the assertion by using
\r
345 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
347 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
349 __asm volatile( "NOP" );
\r
350 __asm volatile( "NOP" );
\r
353 taskEXIT_CRITICAL();
\r
355 /*-----------------------------------------------------------*/
\r
357 static void prvSaveTraceFile( void )
\r
359 FILE* pxOutputFile;
\r
361 pxOutputFile = fopen( "Trace.dump", "wb");
\r
363 if( pxOutputFile != NULL )
\r
365 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
366 fclose( pxOutputFile );
\r
367 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
371 printf( "\r\nFailed to create trace dump file\r\n" );
\r
374 /*-----------------------------------------------------------*/
\r
376 static void prvInitialiseHeap( void )
\r
378 /* This demo uses heap_5.c, so start by defining some heap regions. This is
\r
379 only done to provide an example as this demo could easily create one large heap
\r
380 region instead of multiple smaller heap regions - in which case heap_4.c would
\r
381 be the more appropriate choice. No initialisation is required when heap_4.c is
\r
382 used. The xHeapRegions structure requires the regions to be defined in order,
\r
383 so this just creates one big array, then populates the structure with offsets
\r
384 into the array - with gaps in between and messy alignment just for test
\r
386 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
387 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
388 const HeapRegion_t xHeapRegions[] =
\r
390 /* Start address with dummy offsets Size */
\r
391 { ucHeap + 1, mainREGION_1_SIZE },
\r
392 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
393 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
397 /* Sanity check that the sizes and offsets defined actually fit into the
\r
399 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
401 vPortDefineHeapRegions( xHeapRegions );
\r
403 /*-----------------------------------------------------------*/
\r