2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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 10801
\r
79 #define mainREGION_2_SIZE 29905
\r
80 #define mainREGION_3_SIZE 6007
\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 * Performs a few sanity checks on the behaviour of the vPortGetHeapStats()
\r
110 static void prvExerciseHeapStats( void );
\r
113 * Prototypes for the standard FreeRTOS application hook (callback) functions
\r
114 * implemented within this file. See http://www.freertos.org/a00016.html .
\r
116 void vApplicationMallocFailedHook( void );
\r
117 void vApplicationIdleHook( void );
\r
118 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
119 void vApplicationTickHook( void );
\r
120 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
\r
121 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
\r
124 * Writes trace data to a disk file when the trace recording is stopped.
\r
125 * This function will simply overwrite any trace files that already exist.
\r
127 static void prvSaveTraceFile( void );
\r
129 /*-----------------------------------------------------------*/
\r
131 /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
\r
132 use a callback function to optionally provide the memory required by the idle
\r
133 and timer tasks. This is the stack that will be used by the timer task. It is
\r
134 declared here, as a global, so it can be checked by a test that is implemented
\r
135 in a different file. */
\r
136 StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
\r
138 /* Notes if the trace is running or not. */
\r
139 static BaseType_t xTraceRunning = pdTRUE;
\r
141 /*-----------------------------------------------------------*/
\r
145 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
\r
146 is only used for test and example reasons. Heap_4 is more appropriate. See
\r
147 http://www.freertos.org/a00111.html for an explanation. */
\r
148 prvInitialiseHeap();
\r
150 /* Do not include trace code when performing a code coverage analysis. */
\r
151 #if( projCOVERAGE_TEST != 1 )
\r
153 /* Initialise the trace recorder. Use of the trace recorder is optional.
\r
154 See http://www.FreeRTOS.org/trace for more information. */
\r
155 vTraceEnable( TRC_START );
\r
157 /* Start the trace recording - the recording is written to a file if
\r
158 configASSERT() is called. */
\r
159 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );
\r
160 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );
\r
165 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
167 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
179 /*-----------------------------------------------------------*/
\r
181 void vApplicationMallocFailedHook( void )
\r
183 /* vApplicationMallocFailedHook() will only be called if
\r
184 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
185 function that will get called if a call to pvPortMalloc() fails.
\r
186 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
187 timer or semaphore is created. It is also called by various parts of the
\r
188 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
\r
189 size of the heap available to pvPortMalloc() is defined by
\r
190 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
191 API function can be used to query the size of free heap space that remains
\r
192 (although it does not provide information on how the remaining heap might be
\r
193 fragmented). See http://www.freertos.org/a00111.html for more
\r
195 vAssertCalled( __LINE__, __FILE__ );
\r
197 /*-----------------------------------------------------------*/
\r
199 void vApplicationIdleHook( void )
\r
201 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
202 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
203 task. It is essential that code added to this hook function never attempts
\r
204 to block in any way (for example, call xQueueReceive() with a block time
\r
205 specified, or call vTaskDelay()). If application tasks make use of the
\r
206 vTaskDelete() API function to delete themselves then it is also important
\r
207 that vApplicationIdleHook() is permitted to return to its calling function,
\r
208 because it is the responsibility of the idle task to clean up memory
\r
209 allocated by the kernel to any task that has since deleted itself. */
\r
211 /* Uncomment the following code to allow the trace to be stopped with any
\r
212 key press. The code is commented out by default as the kbhit() function
\r
213 interferes with the run time behaviour. */
\r
215 if( _kbhit() != pdFALSE )
\r
217 if( xTraceRunning == pdTRUE )
\r
220 prvSaveTraceFile();
\r
221 xTraceRunning = pdFALSE;
\r
226 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
228 /* Call the idle task processing used by the full demo. The simple
\r
229 blinky demo does not use the idle task hook. */
\r
230 vFullDemoIdleFunction();
\r
234 /*-----------------------------------------------------------*/
\r
236 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
238 ( void ) pcTaskName;
\r
241 /* Run time stack overflow checking is performed if
\r
242 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
243 function is called if a stack overflow is detected. This function is
\r
244 provided as an example only as stack overflow checking does not function
\r
245 when running the FreeRTOS Windows port. */
\r
246 vAssertCalled( __LINE__, __FILE__ );
\r
248 /*-----------------------------------------------------------*/
\r
250 void vApplicationTickHook( void )
\r
252 /* This function will be called by each tick interrupt if
\r
253 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
254 added here, but the tick hook is called from an interrupt context, so
\r
255 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
256 functions can be used (those that end in FromISR()). */
\r
258 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
260 vFullDemoTickHookFunction();
\r
262 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
264 /*-----------------------------------------------------------*/
\r
266 void vApplicationDaemonTaskStartupHook( void )
\r
268 /* This function will be called once only, when the daemon task starts to
\r
269 execute (sometimes called the timer task). This is useful if the
\r
270 application includes initialisation code that would benefit from executing
\r
271 after the scheduler has been started. */
\r
273 /*-----------------------------------------------------------*/
\r
275 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
277 static BaseType_t xPrinted = pdFALSE;
\r
278 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
280 /* Called if an assertion passed to configASSERT() fails. See
\r
281 http://www.freertos.org/a00110.html#configASSERT for more information. */
\r
283 /* Parameters are not used. */
\r
285 ( void ) pcFileName;
\r
288 taskENTER_CRITICAL();
\r
290 /* Stop the trace recording. */
\r
291 if( xPrinted == pdFALSE )
\r
294 if( xTraceRunning == pdTRUE )
\r
296 prvSaveTraceFile();
\r
300 /* You can step out of this function to debug the assertion by using
\r
301 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
303 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
305 __asm volatile( "NOP" );
\r
306 __asm volatile( "NOP" );
\r
309 taskEXIT_CRITICAL();
\r
311 /*-----------------------------------------------------------*/
\r
313 static void prvSaveTraceFile( void )
\r
315 /* Tracing is not used when code coverage analysis is being performed. */
\r
316 #if( projCOVERAGE_TEST != 1 )
\r
318 FILE* pxOutputFile;
\r
322 pxOutputFile = fopen( "Trace.dump", "wb");
\r
324 if( pxOutputFile != NULL )
\r
326 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
327 fclose( pxOutputFile );
\r
328 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
332 printf( "\r\nFailed to create trace dump file\r\n" );
\r
337 /*-----------------------------------------------------------*/
\r
339 static void prvInitialiseHeap( void )
\r
341 /* The Windows demo could create one large heap region, in which case it would
\r
342 be appropriate to use heap_4. However, purely for demonstration purposes,
\r
343 heap_5 is used instead, so start by defining some heap regions. No
\r
344 initialisation is required when any other heap implementation is used. See
\r
345 http://www.freertos.org/a00111.html for more information.
\r
347 The xHeapRegions structure requires the regions to be defined in start address
\r
348 order, so this just creates one big array, then populates the structure with
\r
349 offsets into the array - with gaps in between and messy alignment just for test
\r
351 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
352 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
353 HeapStats_t xHeapStats;
\r
354 const HeapStats_t xZeroHeapStats = { 0 };
\r
355 const HeapRegion_t xHeapRegions[] =
\r
357 /* Start address with dummy offsets Size */
\r
358 { ucHeap + 1, mainREGION_1_SIZE },
\r
359 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
360 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
364 /* Sanity check that the sizes and offsets defined actually fit into the
\r
366 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
368 /* Prevent compiler warnings when configASSERT() is not defined. */
\r
369 ( void ) ulAdditionalOffset;
\r
371 /* The heap has not been initialised yet so expect stats to all be zero. */
\r
372 vPortGetHeapStats( &xHeapStats );
\r
373 configASSERT( memcmp( &xHeapStats, &xZeroHeapStats, sizeof( HeapStats_t ) ) == 0 );
\r
375 vPortDefineHeapRegions( xHeapRegions );
\r
377 /* Sanity check vTaskGetHeapStats(). */
\r
378 prvExerciseHeapStats();
\r
380 /*-----------------------------------------------------------*/
\r
382 static void prvExerciseHeapStats( void )
\r
384 HeapStats_t xHeapStats;
\r
385 size_t xInitialFreeSpace = xPortGetFreeHeapSize(), xMinimumFreeBytes;
\r
386 size_t xMetaDataOverhead, i;
\r
387 void *pvAllocatedBlock;
\r
388 const size_t xArraySize = 5, xBlockSize = 1000UL;
\r
389 void *pvAllocatedBlocks[ xArraySize ];
\r
391 /* Check heap stats are as expected after initialisation but before any
\r
393 vPortGetHeapStats( &xHeapStats );
\r
395 /* Minimum ever free bytes remaining should be the same as the total number
\r
396 of bytes as nothing has been allocated yet. */
\r
397 configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == xHeapStats.xAvailableHeapSpaceInBytes );
\r
398 configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == xInitialFreeSpace );
\r
400 /* Nothing has been allocated or freed yet. */
\r
401 configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == 0 );
\r
402 configASSERT( xHeapStats.xNumberOfSuccessfulFrees == 0 );
\r
404 /* Allocate a 1000 byte block then measure what the overhead of the
\r
405 allocation in regards to how many bytes more than 1000 were actually
\r
406 removed from the heap in order to store metadata about the allocation. */
\r
407 pvAllocatedBlock = pvPortMalloc( xBlockSize );
\r
408 configASSERT( pvAllocatedBlock );
\r
409 xMetaDataOverhead = ( xInitialFreeSpace - xPortGetFreeHeapSize() ) - xBlockSize;
\r
411 /* Free the block again to get back to where we started. */
\r
412 vPortFree( pvAllocatedBlock );
\r
413 vPortGetHeapStats( &xHeapStats );
\r
414 configASSERT( xHeapStats.xAvailableHeapSpaceInBytes == xInitialFreeSpace );
\r
415 configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == 1 );
\r
416 configASSERT( xHeapStats.xNumberOfSuccessfulFrees == 1 );
\r
418 /* Allocate blocks checking some stats value on each allocation. */
\r
419 for( i = 0; i < xArraySize; i++ )
\r
421 pvAllocatedBlocks[ i ] = pvPortMalloc( xBlockSize );
\r
422 configASSERT( pvAllocatedBlocks[ i ] );
\r
423 vPortGetHeapStats( &xHeapStats );
\r
424 configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == ( xInitialFreeSpace - ( ( i + 1 ) * ( xBlockSize + xMetaDataOverhead ) ) ) );
\r
425 configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == xHeapStats.xAvailableHeapSpaceInBytes );
\r
426 configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == ( 2Ul + i ) );
\r
427 configASSERT( xHeapStats.xNumberOfSuccessfulFrees == 1 ); /* Does not increase during allocations. */
\r
430 configASSERT( xPortGetFreeHeapSize() == xPortGetMinimumEverFreeHeapSize() );
\r
431 xMinimumFreeBytes = xPortGetFreeHeapSize();
\r
433 /* Free the blocks again. */
\r
434 for( i = 0; i < xArraySize; i++ )
\r
436 vPortFree( pvAllocatedBlocks[ i ] );
\r
437 vPortGetHeapStats( &xHeapStats );
\r
438 configASSERT( xHeapStats.xAvailableHeapSpaceInBytes == ( xInitialFreeSpace - ( ( ( xArraySize - i - 1 ) * ( xBlockSize + xMetaDataOverhead ) ) ) ) );
\r
439 configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == ( xArraySize + 1 ) ); /* Does not increase during frees. */
\r
440 configASSERT( xHeapStats.xNumberOfSuccessfulFrees == ( 2UL + i ) );
\r
443 /* The minimum ever free heap size should not change as blocks are freed. */
\r
444 configASSERT( xMinimumFreeBytes == xPortGetMinimumEverFreeHeapSize() );
\r
446 /*-----------------------------------------------------------*/
\r
448 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
\r
449 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
\r
450 used by the Idle task. */
\r
451 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
\r
453 /* If the buffers to be provided to the Idle task are declared inside this
\r
454 function then they must be declared static - otherwise they will be allocated on
\r
455 the stack and so not exists after this function exits. */
\r
456 static StaticTask_t xIdleTaskTCB;
\r
457 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
\r
459 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
\r
460 state will be stored. */
\r
461 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
\r
463 /* Pass out the array that will be used as the Idle task's stack. */
\r
464 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
\r
466 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
\r
467 Note that, as the array is necessarily of type StackType_t,
\r
468 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
469 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
\r
471 /*-----------------------------------------------------------*/
\r
473 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
\r
474 application must provide an implementation of vApplicationGetTimerTaskMemory()
\r
475 to provide the memory that is used by the Timer service task. */
\r
476 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
\r
478 /* If the buffers to be provided to the Timer task are declared inside this
\r
479 function then they must be declared static - otherwise they will be allocated on
\r
480 the stack and so not exists after this function exits. */
\r
481 static StaticTask_t xTimerTaskTCB;
\r
483 /* Pass out a pointer to the StaticTask_t structure in which the Timer
\r
484 task's state will be stored. */
\r
485 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
\r
487 /* Pass out the array that will be used as the Timer task's stack. */
\r
488 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
\r
490 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
\r
491 Note that, as the array is necessarily of type StackType_t,
\r
492 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
493 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
\r