2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
37 * This file implements the code that is not demo specific, including the
\r
38 * hardware setup and FreeRTOS hook functions.
\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
50 *******************************************************************************
\r
53 /* Standard includes. */
\r
58 /* FreeRTOS kernel includes. */
\r
59 #include "FreeRTOS.h"
\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
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
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
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
83 /*-----------------------------------------------------------*/
\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
89 extern void main_blinky( void );
\r
90 extern void main_full( void );
\r
93 * Only the comprehensive demo uses application hook (callback) functions. See
\r
94 * http://www.freertos.org/a00016.html for more information.
\r
96 void vFullDemoTickHookFunction( void );
\r
97 void vFullDemoIdleFunction( void );
\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
105 static void prvInitialiseHeap( void );
\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
111 void vApplicationMallocFailedHook( void );
\r
112 void vApplicationIdleHook( void );
\r
113 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
114 void vApplicationTickHook( void );
\r
117 * Writes trace data to a disk file when the trace recording is stopped.
\r
118 * This function will simply overwrite any trace files that already exist.
\r
120 static void prvSaveTraceFile( void );
\r
122 /* Notes if the trace is running or not. */
\r
123 static BaseType_t xTraceRunning = pdTRUE;
\r
125 /*-----------------------------------------------------------*/
\r
129 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
\r
130 is only used for test and example reasons. Heap_4 is more appropriate. See
\r
131 http://www.freertos.org/a00111.html for an explanation. */
\r
132 prvInitialiseHeap();
\r
134 /* Initialise the trace recorder. Use of the trace recorder is optional.
\r
135 See http://www.FreeRTOS.org/trace for more information. */
\r
136 vTraceEnable( TRC_START );
\r
138 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
140 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
146 /* Start the trace recording - the recording is written to a file if
\r
147 configASSERT() is called. */
\r
148 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
\r
149 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
\r
158 /*-----------------------------------------------------------*/
\r
160 void vApplicationMallocFailedHook( void )
\r
162 /* vApplicationMallocFailedHook() will only be called if
\r
163 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
164 function that will get called if a call to pvPortMalloc() fails.
\r
165 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
166 timer or semaphore is created. It is also called by various parts of the
\r
167 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
\r
168 size of the heap available to pvPortMalloc() is defined by
\r
169 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
170 API function can be used to query the size of free heap space that remains
\r
171 (although it does not provide information on how the remaining heap might be
\r
172 fragmented). See http://www.freertos.org/a00111.html for more
\r
174 vAssertCalled( __LINE__, __FILE__ );
\r
176 /*-----------------------------------------------------------*/
\r
178 void vApplicationIdleHook( void )
\r
180 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
181 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
182 task. It is essential that code added to this hook function never attempts
\r
183 to block in any way (for example, call xQueueReceive() with a block time
\r
184 specified, or call vTaskDelay()). If application tasks make use of the
\r
185 vTaskDelete() API function to delete themselves then it is also important
\r
186 that vApplicationIdleHook() is permitted to return to its calling function,
\r
187 because it is the responsibility of the idle task to clean up memory
\r
188 allocated by the kernel to any task that has since deleted itself. */
\r
190 /* Uncomment the following code to allow the trace to be stopped with any
\r
191 key press. The code is commented out by default as the kbhit() function
\r
192 interferes with the run time behaviour. */
\r
194 if( _kbhit() != pdFALSE )
\r
196 if( xTraceRunning == pdTRUE )
\r
199 prvSaveTraceFile();
\r
200 xTraceRunning = pdFALSE;
\r
205 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
207 /* Call the idle task processing used by the full demo. The simple
\r
208 blinky demo does not use the idle task hook. */
\r
209 vFullDemoIdleFunction();
\r
213 /*-----------------------------------------------------------*/
\r
215 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
217 ( void ) pcTaskName;
\r
220 /* Run time stack overflow checking is performed if
\r
221 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
222 function is called if a stack overflow is detected. This function is
\r
223 provided as an example only as stack overflow checking does not function
\r
224 when running the FreeRTOS Windows port. */
\r
225 vAssertCalled( __LINE__, __FILE__ );
\r
227 /*-----------------------------------------------------------*/
\r
229 void vApplicationTickHook( void )
\r
231 /* This function will be called by each tick interrupt if
\r
232 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
233 added here, but the tick hook is called from an interrupt context, so
\r
234 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
235 functions can be used (those that end in FromISR()). */
\r
237 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
239 vFullDemoTickHookFunction();
\r
241 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
243 /*-----------------------------------------------------------*/
\r
245 void vApplicationDaemonTaskStartupHook( void )
\r
247 /* This function will be called once only, when the daemon task starts to
\r
248 execute (sometimes called the timer task). This is useful if the
\r
249 application includes initialisation code that would benefit from executing
\r
250 after the scheduler has been started. */
\r
252 /*-----------------------------------------------------------*/
\r
254 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
256 static BaseType_t xPrinted = pdFALSE;
\r
257 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
259 /* Called if an assertion passed to configASSERT() fails. See
\r
260 http://www.freertos.org/a00110.html#configASSERT for more information. */
\r
262 /* Parameters are not used. */
\r
264 ( void ) pcFileName;
\r
267 taskENTER_CRITICAL();
\r
269 /* Stop the trace recording. */
\r
270 if( xPrinted == pdFALSE )
\r
273 if( xTraceRunning == pdTRUE )
\r
276 prvSaveTraceFile();
\r
280 /* You can step out of this function to debug the assertion by using
\r
281 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
283 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
285 __asm volatile( "NOP" );
\r
286 __asm volatile( "NOP" );
\r
289 taskEXIT_CRITICAL();
\r
291 /*-----------------------------------------------------------*/
\r
293 static void prvSaveTraceFile( void )
\r
295 FILE* pxOutputFile;
\r
297 pxOutputFile = fopen( "Trace.dump", "wb");
\r
299 if( pxOutputFile != NULL )
\r
301 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
302 fclose( pxOutputFile );
\r
303 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
307 printf( "\r\nFailed to create trace dump file\r\n" );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvInitialiseHeap( void )
\r
314 /* The Windows demo could create one large heap region, in which case it would
\r
315 be appropriate to use heap_4. However, purely for demonstration purposes,
\r
316 heap_5 is used instead, so start by defining some heap regions. No
\r
317 initialisation is required when any other heap implementation is used. See
\r
318 http://www.freertos.org/a00111.html for more information.
\r
320 The xHeapRegions structure requires the regions to be defined in start address
\r
321 order, so this just creates one big array, then populates the structure with
\r
322 offsets into the array - with gaps in between and messy alignment just for test
\r
324 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
325 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
326 const HeapRegion_t xHeapRegions[] =
\r
328 /* Start address with dummy offsets Size */
\r
329 { ucHeap + 1, mainREGION_1_SIZE },
\r
330 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
331 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
335 /* Sanity check that the sizes and offsets defined actually fit into the
\r
337 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
339 /* Prevent compiler warnings when configASSERT() is not defined. */
\r
340 ( void ) ulAdditionalOffset;
\r
342 vPortDefineHeapRegions( xHeapRegions );
\r
344 /*-----------------------------------------------------------*/
\r