2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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 1
\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 8201
\r
79 #define mainREGION_2_SIZE 29905
\r
80 #define mainREGION_3_SIZE 7607
\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
114 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
\r
115 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
\r
118 * Writes trace data to a disk file when the trace recording is stopped.
\r
119 * This function will simply overwrite any trace files that already exist.
\r
121 static void prvSaveTraceFile( void );
\r
123 /*-----------------------------------------------------------*/
\r
125 /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
\r
126 use a callback function to optionally provide the memory required by the idle
\r
127 and timer tasks. This is the stack that will be used by the timer task. It is
\r
128 declared here, as a global, so it can be checked by a test that is implemented
\r
129 in a different file. */
\r
130 StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
\r
132 /* Notes if the trace is running or not. */
\r
133 static BaseType_t xTraceRunning = pdTRUE;
\r
135 /*-----------------------------------------------------------*/
\r
139 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
\r
140 is only used for test and example reasons. Heap_4 is more appropriate. See
\r
141 http://www.freertos.org/a00111.html for an explanation. */
\r
142 prvInitialiseHeap();
\r
144 /* Initialise the trace recorder. Use of the trace recorder is optional.
\r
145 See http://www.FreeRTOS.org/trace for more information. */
\r
146 vTraceEnable( TRC_START );
\r
148 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
150 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
156 /* Start the trace recording - the recording is written to a file if
\r
157 configASSERT() is called. */
\r
158 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
\r
159 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
\r
168 /*-----------------------------------------------------------*/
\r
170 void vApplicationMallocFailedHook( void )
\r
172 /* vApplicationMallocFailedHook() will only be called if
\r
173 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
174 function that will get called if a call to pvPortMalloc() fails.
\r
175 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
176 timer or semaphore is created. It is also called by various parts of the
\r
177 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
\r
178 size of the heap available to pvPortMalloc() is defined by
\r
179 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
180 API function can be used to query the size of free heap space that remains
\r
181 (although it does not provide information on how the remaining heap might be
\r
182 fragmented). See http://www.freertos.org/a00111.html for more
\r
184 vAssertCalled( __LINE__, __FILE__ );
\r
186 /*-----------------------------------------------------------*/
\r
188 void vApplicationIdleHook( void )
\r
190 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
191 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
192 task. It is essential that code added to this hook function never attempts
\r
193 to block in any way (for example, call xQueueReceive() with a block time
\r
194 specified, or call vTaskDelay()). If application tasks make use of the
\r
195 vTaskDelete() API function to delete themselves then it is also important
\r
196 that vApplicationIdleHook() is permitted to return to its calling function,
\r
197 because it is the responsibility of the idle task to clean up memory
\r
198 allocated by the kernel to any task that has since deleted itself. */
\r
200 /* Uncomment the following code to allow the trace to be stopped with any
\r
201 key press. The code is commented out by default as the kbhit() function
\r
202 interferes with the run time behaviour. */
\r
204 if( _kbhit() != pdFALSE )
\r
206 if( xTraceRunning == pdTRUE )
\r
209 prvSaveTraceFile();
\r
210 xTraceRunning = pdFALSE;
\r
215 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
217 /* Call the idle task processing used by the full demo. The simple
\r
218 blinky demo does not use the idle task hook. */
\r
219 vFullDemoIdleFunction();
\r
223 /*-----------------------------------------------------------*/
\r
225 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
227 ( void ) pcTaskName;
\r
230 /* Run time stack overflow checking is performed if
\r
231 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
232 function is called if a stack overflow is detected. This function is
\r
233 provided as an example only as stack overflow checking does not function
\r
234 when running the FreeRTOS Windows port. */
\r
235 vAssertCalled( __LINE__, __FILE__ );
\r
237 /*-----------------------------------------------------------*/
\r
239 void vApplicationTickHook( void )
\r
241 /* This function will be called by each tick interrupt if
\r
242 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
243 added here, but the tick hook is called from an interrupt context, so
\r
244 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
245 functions can be used (those that end in FromISR()). */
\r
247 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
249 vFullDemoTickHookFunction();
\r
251 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
253 /*-----------------------------------------------------------*/
\r
255 void vApplicationDaemonTaskStartupHook( void )
\r
257 /* This function will be called once only, when the daemon task starts to
\r
258 execute (sometimes called the timer task). This is useful if the
\r
259 application includes initialisation code that would benefit from executing
\r
260 after the scheduler has been started. */
\r
262 /*-----------------------------------------------------------*/
\r
264 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
266 static BaseType_t xPrinted = pdFALSE;
\r
267 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
269 /* Called if an assertion passed to configASSERT() fails. See
\r
270 http://www.freertos.org/a00110.html#configASSERT for more information. */
\r
272 /* Parameters are not used. */
\r
274 ( void ) pcFileName;
\r
276 printf( "ASSERT! Line %ld, file %s, GetLastError() %ld\r\n", ulLine, pcFileName, GetLastError() );
\r
278 taskENTER_CRITICAL();
\r
280 /* Stop the trace recording. */
\r
281 if( xPrinted == pdFALSE )
\r
284 if( xTraceRunning == pdTRUE )
\r
287 prvSaveTraceFile();
\r
291 /* You can step out of this function to debug the assertion by using
\r
292 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
294 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
300 taskEXIT_CRITICAL();
\r
302 /*-----------------------------------------------------------*/
\r
304 static void prvSaveTraceFile( void )
\r
306 FILE* pxOutputFile;
\r
308 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
310 if( pxOutputFile != NULL )
\r
312 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
313 fclose( pxOutputFile );
\r
314 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
318 printf( "\r\nFailed to create trace dump file\r\n" );
\r
321 /*-----------------------------------------------------------*/
\r
323 static void prvInitialiseHeap( void )
\r
325 /* The Windows demo could create one large heap region, in which case it would
\r
326 be appropriate to use heap_4. However, purely for demonstration purposes,
\r
327 heap_5 is used instead, so start by defining some heap regions. No
\r
328 initialisation is required when any other heap implementation is used. See
\r
329 http://www.freertos.org/a00111.html for more information.
\r
331 The xHeapRegions structure requires the regions to be defined in start address
\r
332 order, so this just creates one big array, then populates the structure with
\r
333 offsets into the array - with gaps in between and messy alignment just for test
\r
335 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
336 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
337 const HeapRegion_t xHeapRegions[] =
\r
339 /* Start address with dummy offsets Size */
\r
340 { ucHeap + 1, mainREGION_1_SIZE },
\r
341 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
342 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
346 /* Sanity check that the sizes and offsets defined actually fit into the
\r
348 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
350 /* Prevent compiler warnings when configASSERT() is not defined. */
\r
351 ( void ) ulAdditionalOffset;
\r
353 vPortDefineHeapRegions( xHeapRegions );
\r
355 /*-----------------------------------------------------------*/
\r
357 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
\r
358 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
\r
359 used by the Idle task. */
\r
360 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
\r
362 /* If the buffers to be provided to the Idle task are declared inside this
\r
363 function then they must be declared static - otherwise they will be allocated on
\r
364 the stack and so not exists after this function exits. */
\r
365 static StaticTask_t xIdleTaskTCB;
\r
366 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
\r
368 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
\r
369 state will be stored. */
\r
370 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
\r
372 /* Pass out the array that will be used as the Idle task's stack. */
\r
373 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
\r
375 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
\r
376 Note that, as the array is necessarily of type StackType_t,
\r
377 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
378 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
\r
380 /*-----------------------------------------------------------*/
\r
382 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
\r
383 application must provide an implementation of vApplicationGetTimerTaskMemory()
\r
384 to provide the memory that is used by the Timer service task. */
\r
385 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
\r
387 /* If the buffers to be provided to the Timer task are declared inside this
\r
388 function then they must be declared static - otherwise they will be allocated on
\r
389 the stack and so not exists after this function exits. */
\r
390 static StaticTask_t xTimerTaskTCB;
\r
392 /* Pass out a pointer to the StaticTask_t structure in which the Timer
\r
393 task's state will be stored. */
\r
394 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
\r
396 /* Pass out the array that will be used as the Timer task's stack. */
\r
397 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
\r
399 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
\r
400 Note that, as the array is necessarily of type StackType_t,
\r
401 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
402 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
\r