2 * FreeRTOS Kernel V10.0.1
\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.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\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
36 * This file implements the code that is not demo specific, including the
\r
37 * hardware setup and FreeRTOS hook functions.
\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
49 *******************************************************************************
\r
52 /* Standard includes. */
\r
57 /* FreeRTOS kernel includes. */
\r
58 #include "FreeRTOS.h"
\r
61 /* This project provides two demo applications. A simple blinky style demo
\r
62 application, and a more comprehensive test and demo application. The
\r
63 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
\r
65 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is 1 then the blinky demo will be built.
\r
66 The blinky demo is implemented and described in main_blinky.c.
\r
68 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not 1 then the comprehensive test and
\r
69 demo application will be built. The comprehensive test and demo application is
\r
70 implemented and described in main_full.c. */
\r
71 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 0
\r
73 /* This demo uses heap_5.c, and these constants define the sizes of the regions
\r
74 that make up the total heap. heap_5 is only used for test and example purposes
\r
75 as this demo could easily create one large heap region instead of multiple
\r
76 smaller heap regions - in which case heap_4.c would be the more appropriate
\r
77 choice. See http://www.freertos.org/a00111.html for an explanation. */
\r
78 #define mainREGION_1_SIZE 7201
\r
79 #define mainREGION_2_SIZE 29905
\r
80 #define mainREGION_3_SIZE 6407
\r
82 /*-----------------------------------------------------------*/
\r
85 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
86 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
88 extern void main_blinky( void );
\r
89 extern void main_full( void );
\r
92 * Only the comprehensive demo uses application hook (callback) functions. See
\r
93 * http://www.freertos.org/a00016.html for more information.
\r
95 void vFullDemoTickHookFunction( void );
\r
96 void vFullDemoIdleFunction( void );
\r
99 * This demo uses heap_5.c, so start by defining some heap regions. It is not
\r
100 * necessary for this demo to use heap_5, as it could define one large heap
\r
101 * region. Heap_5 is only used for test and example purposes. See
\r
102 * http://www.freertos.org/a00111.html for an explanation.
\r
104 static void prvInitialiseHeap( void );
\r
107 * Prototypes for the standard FreeRTOS application hook (callback) functions
\r
108 * implemented within this file. See http://www.freertos.org/a00016.html .
\r
110 void vApplicationMallocFailedHook( void );
\r
111 void vApplicationIdleHook( void );
\r
112 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
113 void vApplicationTickHook( void );
\r
116 * Writes trace data to a disk file when the trace recording is stopped.
\r
117 * This function will simply overwrite any trace files that already exist.
\r
119 static void prvSaveTraceFile( void );
\r
121 /* Notes if the trace is running or not. */
\r
122 static BaseType_t xTraceRunning = pdTRUE;
\r
124 /*-----------------------------------------------------------*/
\r
128 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
\r
129 is only used for test and example reasons. Heap_4 is more appropriate. See
\r
130 http://www.freertos.org/a00111.html for an explanation. */
\r
131 prvInitialiseHeap();
\r
133 /* Initialise the trace recorder. Use of the trace recorder is optional.
\r
134 See http://www.FreeRTOS.org/trace for more information. */
\r
135 vTraceEnable( TRC_START );
\r
137 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
139 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
145 /* Start the trace recording - the recording is written to a file if
\r
146 configASSERT() is called. */
\r
147 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
\r
148 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
\r
157 /*-----------------------------------------------------------*/
\r
159 void vApplicationMallocFailedHook( void )
\r
161 /* vApplicationMallocFailedHook() will only be called if
\r
162 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
163 function that will get called if a call to pvPortMalloc() fails.
\r
164 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
165 timer or semaphore is created. It is also called by various parts of the
\r
166 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
\r
167 size of the heap available to pvPortMalloc() is defined by
\r
168 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
169 API function can be used to query the size of free heap space that remains
\r
170 (although it does not provide information on how the remaining heap might be
\r
171 fragmented). See http://www.freertos.org/a00111.html for more
\r
173 vAssertCalled( __LINE__, __FILE__ );
\r
175 /*-----------------------------------------------------------*/
\r
177 void vApplicationIdleHook( void )
\r
179 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
180 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
181 task. It is essential that code added to this hook function never attempts
\r
182 to block in any way (for example, call xQueueReceive() with a block time
\r
183 specified, or call vTaskDelay()). If application tasks make use of the
\r
184 vTaskDelete() API function to delete themselves then it is also important
\r
185 that vApplicationIdleHook() is permitted to return to its calling function,
\r
186 because it is the responsibility of the idle task to clean up memory
\r
187 allocated by the kernel to any task that has since deleted itself. */
\r
189 /* Uncomment the following code to allow the trace to be stopped with any
\r
190 key press. The code is commented out by default as the kbhit() function
\r
191 interferes with the run time behaviour. */
\r
193 if( _kbhit() != pdFALSE )
\r
195 if( xTraceRunning == pdTRUE )
\r
198 prvSaveTraceFile();
\r
199 xTraceRunning = pdFALSE;
\r
204 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
206 /* Call the idle task processing used by the full demo. The simple
\r
207 blinky demo does not use the idle task hook. */
\r
208 vFullDemoIdleFunction();
\r
212 /*-----------------------------------------------------------*/
\r
214 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
216 ( void ) pcTaskName;
\r
219 /* Run time stack overflow checking is performed if
\r
220 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
221 function is called if a stack overflow is detected. This function is
\r
222 provided as an example only as stack overflow checking does not function
\r
223 when running the FreeRTOS Windows port. */
\r
224 vAssertCalled( __LINE__, __FILE__ );
\r
226 /*-----------------------------------------------------------*/
\r
228 void vApplicationTickHook( void )
\r
230 /* This function will be called by each tick interrupt if
\r
231 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
232 added here, but the tick hook is called from an interrupt context, so
\r
233 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
234 functions can be used (those that end in FromISR()). */
\r
236 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
238 vFullDemoTickHookFunction();
\r
240 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
242 /*-----------------------------------------------------------*/
\r
244 void vApplicationDaemonTaskStartupHook( void )
\r
246 /* This function will be called once only, when the daemon task starts to
\r
247 execute (sometimes called the timer task). This is useful if the
\r
248 application includes initialisation code that would benefit from executing
\r
249 after the scheduler has been started. */
\r
251 /*-----------------------------------------------------------*/
\r
253 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
255 static BaseType_t xPrinted = pdFALSE;
\r
256 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
258 /* Called if an assertion passed to configASSERT() fails. See
\r
259 http://www.freertos.org/a00110.html#configASSERT for more information. */
\r
261 /* Parameters are not used. */
\r
263 ( void ) pcFileName;
\r
266 taskENTER_CRITICAL();
\r
268 /* Stop the trace recording. */
\r
269 if( xPrinted == pdFALSE )
\r
272 if( xTraceRunning == pdTRUE )
\r
275 prvSaveTraceFile();
\r
279 /* You can step out of this function to debug the assertion by using
\r
280 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
282 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
284 __asm volatile( "NOP" );
\r
285 __asm volatile( "NOP" );
\r
288 taskEXIT_CRITICAL();
\r
290 /*-----------------------------------------------------------*/
\r
292 static void prvSaveTraceFile( void )
\r
294 FILE* pxOutputFile;
\r
296 pxOutputFile = fopen( "Trace.dump", "wb");
\r
298 if( pxOutputFile != NULL )
\r
300 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
301 fclose( pxOutputFile );
\r
302 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
306 printf( "\r\nFailed to create trace dump file\r\n" );
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvInitialiseHeap( void )
\r
313 /* The Windows demo could create one large heap region, in which case it would
\r
314 be appropriate to use heap_4. However, purely for demonstration purposes,
\r
315 heap_5 is used instead, so start by defining some heap regions. No
\r
316 initialisation is required when any other heap implementation is used. See
\r
317 http://www.freertos.org/a00111.html for more information.
\r
319 The xHeapRegions structure requires the regions to be defined in start address
\r
320 order, so this just creates one big array, then populates the structure with
\r
321 offsets into the array - with gaps in between and messy alignment just for test
\r
323 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
324 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
325 const HeapRegion_t xHeapRegions[] =
\r
327 /* Start address with dummy offsets Size */
\r
328 { ucHeap + 1, mainREGION_1_SIZE },
\r
329 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
330 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
334 /* Sanity check that the sizes and offsets defined actually fit into the
\r
336 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
338 /* Prevent compiler warnings when configASSERT() is not defined. */
\r
339 ( void ) ulAdditionalOffset;
\r
341 vPortDefineHeapRegions( xHeapRegions );
\r
343 /*-----------------------------------------------------------*/
\r