2 * FreeRTOS Kernel V10.3.0
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
28 /******************************************************************************
29 * This project provides two demo applications. A simple blinky style project,
30 * and a more comprehensive test and demo application. The
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
32 * The simply blinky demo is implemented and described in main_blinky.c. The
33 * more comprehensive test and demo application is implemented and described in
36 * This file implements the code that is not demo specific, including the
37 * hardware setup and FreeRTOS hook functions.
39 *******************************************************************************
40 * NOTE: Windows will not be running the FreeRTOS demo threads continuously, so
41 * do not expect to get real time behaviour from the FreeRTOS Windows port, or
42 * this demo application. Also, the timing information in the FreeRTOS+Trace
43 * logs have no meaningful units. See the documentation page for the Windows
44 * port for further information:
45 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
49 *******************************************************************************
52 /* Standard includes. */
57 /* FreeRTOS kernel includes. */
64 /* This project provides two demo applications. A simple blinky style demo
65 application, and a more comprehensive test and demo application. The
66 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.
68 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is 1 then the blinky demo will be built.
69 The blinky demo is implemented and described in main_blinky.c.
71 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not 1 then the comprehensive test and
72 demo application will be built. The comprehensive test and demo application is
73 implemented and described in main_full.c. */
74 #ifndef mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
75 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 0
78 /* This demo uses heap_3.c (the libc provided malloc() and free()). */
80 /*-----------------------------------------------------------*/
83 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
84 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
86 extern void main_blinky( void );
87 extern void main_full( void );
90 * Only the comprehensive demo uses application hook (callback) functions. See
91 * http://www.freertos.org/a00016.html for more information.
93 void vFullDemoTickHookFunction( void );
94 void vFullDemoIdleFunction( void );
97 * Prototypes for the standard FreeRTOS application hook (callback) functions
98 * implemented within this file. See http://www.freertos.org/a00016.html .
100 void vApplicationMallocFailedHook( void );
101 void vApplicationIdleHook( void );
102 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
103 void vApplicationTickHook( void );
104 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
105 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
108 * Writes trace data to a disk file when the trace recording is stopped.
109 * This function will simply overwrite any trace files that already exist.
111 static void prvSaveTraceFile( void );
113 /*-----------------------------------------------------------*/
115 /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
116 use a callback function to optionally provide the memory required by the idle
117 and timer tasks. This is the stack that will be used by the timer task. It is
118 declared here, as a global, so it can be checked by a test that is implemented
119 in a different file. */
120 StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
122 /* Notes if the trace is running or not. */
123 static BaseType_t xTraceRunning = pdTRUE;
125 /*-----------------------------------------------------------*/
129 /* Do not include trace code when performing a code coverage analysis. */
130 #if( projCOVERAGE_TEST != 1 )
132 /* Initialise the trace recorder. Use of the trace recorder is optional.
133 See http://www.FreeRTOS.org/trace for more information. */
134 vTraceEnable( TRC_START );
136 /* Start the trace recording - the recording is written to a file if
137 configASSERT() is called. */
138 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
139 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
146 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
148 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
160 /*-----------------------------------------------------------*/
162 void vApplicationMallocFailedHook( void )
164 /* vApplicationMallocFailedHook() will only be called if
165 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
166 function that will get called if a call to pvPortMalloc() fails.
167 pvPortMalloc() is called internally by the kernel whenever a task, queue,
168 timer or semaphore is created. It is also called by various parts of the
169 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
170 size of the heap available to pvPortMalloc() is defined by
171 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
172 API function can be used to query the size of free heap space that remains
173 (although it does not provide information on how the remaining heap might be
174 fragmented). See http://www.freertos.org/a00111.html for more
176 vAssertCalled( __LINE__, __FILE__ );
178 /*-----------------------------------------------------------*/
180 void vApplicationIdleHook( void )
182 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
183 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
184 task. It is essential that code added to this hook function never attempts
185 to block in any way (for example, call xQueueReceive() with a block time
186 specified, or call vTaskDelay()). If application tasks make use of the
187 vTaskDelete() API function to delete themselves then it is also important
188 that vApplicationIdleHook() is permitted to return to its calling function,
189 because it is the responsibility of the idle task to clean up memory
190 allocated by the kernel to any task that has since deleted itself. */
194 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
196 /* Call the idle task processing used by the full demo. The simple
197 blinky demo does not use the idle task hook. */
198 vFullDemoIdleFunction();
202 /*-----------------------------------------------------------*/
204 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
209 /* Run time stack overflow checking is performed if
210 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
211 function is called if a stack overflow is detected. This function is
212 provided as an example only as stack overflow checking does not function
213 when running the FreeRTOS Windows port. */
214 vAssertCalled( __LINE__, __FILE__ );
216 /*-----------------------------------------------------------*/
218 void vApplicationTickHook( void )
220 /* This function will be called by each tick interrupt if
221 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
222 added here, but the tick hook is called from an interrupt context, so
223 code must not attempt to block, and only the interrupt safe FreeRTOS API
224 functions can be used (those that end in FromISR()). */
226 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
228 vFullDemoTickHookFunction();
230 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
232 /*-----------------------------------------------------------*/
234 void vApplicationDaemonTaskStartupHook( void )
236 /* This function will be called once only, when the daemon task starts to
237 execute (sometimes called the timer task). This is useful if the
238 application includes initialisation code that would benefit from executing
239 after the scheduler has been started. */
241 /*-----------------------------------------------------------*/
243 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
245 static BaseType_t xPrinted = pdFALSE;
246 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
248 /* Called if an assertion passed to configASSERT() fails. See
249 http://www.freertos.org/a00110.html#configASSERT for more information. */
251 /* Parameters are not used. */
256 taskENTER_CRITICAL();
258 /* Stop the trace recording. */
259 if( xPrinted == pdFALSE )
262 if( xTraceRunning == pdTRUE )
268 /* You can step out of this function to debug the assertion by using
269 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
271 while( ulSetToNonZeroInDebuggerToContinue == 0 )
273 __asm volatile( "NOP" );
274 __asm volatile( "NOP" );
279 /*-----------------------------------------------------------*/
281 static void prvSaveTraceFile( void )
283 /* Tracing is not used when code coverage analysis is being performed. */
284 #if( projCOVERAGE_TEST != 1 )
290 pxOutputFile = fopen( "Trace.dump", "wb");
292 if( pxOutputFile != NULL )
294 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
295 fclose( pxOutputFile );
296 printf( "\r\nTrace output saved to Trace.dump\r\n" );
300 printf( "\r\nFailed to create trace dump file\r\n" );
305 /*-----------------------------------------------------------*/
307 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
308 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
309 used by the Idle task. */
310 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
312 /* If the buffers to be provided to the Idle task are declared inside this
313 function then they must be declared static - otherwise they will be allocated on
314 the stack and so not exists after this function exits. */
315 static StaticTask_t xIdleTaskTCB;
316 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
318 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
319 state will be stored. */
320 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
322 /* Pass out the array that will be used as the Idle task's stack. */
323 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
325 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
326 Note that, as the array is necessarily of type StackType_t,
327 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
328 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
330 /*-----------------------------------------------------------*/
332 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
333 application must provide an implementation of vApplicationGetTimerTaskMemory()
334 to provide the memory that is used by the Timer service task. */
335 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
337 /* If the buffers to be provided to the Timer task are declared inside this
338 function then they must be declared static - otherwise they will be allocated on
339 the stack and so not exists after this function exits. */
340 static StaticTask_t xTimerTaskTCB;
342 /* Pass out a pointer to the StaticTask_t structure in which the Timer
343 task's state will be stored. */
344 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
346 /* Pass out the array that will be used as the Timer task's stack. */
347 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
349 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
350 Note that, as the array is necessarily of type StackType_t,
351 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
352 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;