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
115 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
\r
116 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
\r
119 * Writes trace data to a disk file when the trace recording is stopped.
\r
120 * This function will simply overwrite any trace files that already exist.
\r
122 static void prvSaveTraceFile( void );
\r
124 /*-----------------------------------------------------------*/
\r
126 /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
\r
127 use a callback function to optionally provide the memory required by the idle
\r
128 and timer tasks. This is the stack that will be used by the timer task. It is
\r
129 declared here, as a global, so it can be checked by a test that is implemented
\r
130 in a different file. */
\r
131 StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
\r
133 /* Notes if the trace is running or not. */
\r
134 static BaseType_t xTraceRunning = pdTRUE;
\r
136 /*-----------------------------------------------------------*/
\r
140 /* This demo uses heap_5.c, so start by defining some heap regions. heap_5
\r
141 is only used for test and example reasons. Heap_4 is more appropriate. See
\r
142 http://www.freertos.org/a00111.html for an explanation. */
\r
143 prvInitialiseHeap();
\r
145 /* Initialise the trace recorder. Use of the trace recorder is optional.
\r
146 See http://www.FreeRTOS.org/trace for more information. */
\r
147 vTraceEnable( TRC_START );
\r
149 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
151 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\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
169 /*-----------------------------------------------------------*/
\r
171 void vApplicationMallocFailedHook( void )
\r
173 /* vApplicationMallocFailedHook() will only be called if
\r
174 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
175 function that will get called if a call to pvPortMalloc() fails.
\r
176 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
177 timer or semaphore is created. It is also called by various parts of the
\r
178 demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
\r
179 size of the heap available to pvPortMalloc() is defined by
\r
180 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
181 API function can be used to query the size of free heap space that remains
\r
182 (although it does not provide information on how the remaining heap might be
\r
183 fragmented). See http://www.freertos.org/a00111.html for more
\r
185 vAssertCalled( __LINE__, __FILE__ );
\r
187 /*-----------------------------------------------------------*/
\r
189 void vApplicationIdleHook( void )
\r
191 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
192 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
193 task. It is essential that code added to this hook function never attempts
\r
194 to block in any way (for example, call xQueueReceive() with a block time
\r
195 specified, or call vTaskDelay()). If application tasks make use of the
\r
196 vTaskDelete() API function to delete themselves then it is also important
\r
197 that vApplicationIdleHook() is permitted to return to its calling function,
\r
198 because it is the responsibility of the idle task to clean up memory
\r
199 allocated by the kernel to any task that has since deleted itself. */
\r
201 /* Uncomment the following code to allow the trace to be stopped with any
\r
202 key press. The code is commented out by default as the kbhit() function
\r
203 interferes with the run time behaviour. */
\r
205 if( _kbhit() != pdFALSE )
\r
207 if( xTraceRunning == pdTRUE )
\r
210 prvSaveTraceFile();
\r
211 xTraceRunning = pdFALSE;
\r
216 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
218 /* Call the idle task processing used by the full demo. The simple
\r
219 blinky demo does not use the idle task hook. */
\r
220 vFullDemoIdleFunction();
\r
224 /*-----------------------------------------------------------*/
\r
226 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
228 ( void ) pcTaskName;
\r
231 /* Run time stack overflow checking is performed if
\r
232 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
233 function is called if a stack overflow is detected. This function is
\r
234 provided as an example only as stack overflow checking does not function
\r
235 when running the FreeRTOS Windows port. */
\r
236 vAssertCalled( __LINE__, __FILE__ );
\r
238 /*-----------------------------------------------------------*/
\r
240 void vApplicationTickHook( void )
\r
242 /* This function will be called by each tick interrupt if
\r
243 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
244 added here, but the tick hook is called from an interrupt context, so
\r
245 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
246 functions can be used (those that end in FromISR()). */
\r
248 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
250 vFullDemoTickHookFunction();
\r
252 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
\r
254 /*-----------------------------------------------------------*/
\r
256 void vApplicationDaemonTaskStartupHook( void )
\r
258 /* This function will be called once only, when the daemon task starts to
\r
259 execute (sometimes called the timer task). This is useful if the
\r
260 application includes initialisation code that would benefit from executing
\r
261 after the scheduler has been started. */
\r
263 /*-----------------------------------------------------------*/
\r
265 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
\r
267 static BaseType_t xPrinted = pdFALSE;
\r
268 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
\r
270 /* Called if an assertion passed to configASSERT() fails. See
\r
271 http://www.freertos.org/a00110.html#configASSERT for more information. */
\r
273 /* Parameters are not used. */
\r
275 ( void ) pcFileName;
\r
277 printf( "ASSERT! Line %ld, file %s, GetLastError() %ld\r\n", ulLine, pcFileName, GetLastError() );
\r
279 taskENTER_CRITICAL();
\r
281 /* Stop the trace recording. */
\r
282 if( xPrinted == pdFALSE )
\r
285 if( xTraceRunning == pdTRUE )
\r
288 prvSaveTraceFile();
\r
292 /* You can step out of this function to debug the assertion by using
\r
293 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
\r
295 while( ulSetToNonZeroInDebuggerToContinue == 0 )
\r
301 taskEXIT_CRITICAL();
\r
303 /*-----------------------------------------------------------*/
\r
305 static void prvSaveTraceFile( void )
\r
307 FILE* pxOutputFile;
\r
309 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
311 if( pxOutputFile != NULL )
\r
313 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
314 fclose( pxOutputFile );
\r
315 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
319 printf( "\r\nFailed to create trace dump file\r\n" );
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvInitialiseHeap( void )
\r
326 /* The Windows demo could create one large heap region, in which case it would
\r
327 be appropriate to use heap_4. However, purely for demonstration purposes,
\r
328 heap_5 is used instead, so start by defining some heap regions. No
\r
329 initialisation is required when any other heap implementation is used. See
\r
330 http://www.freertos.org/a00111.html for more information.
\r
332 The xHeapRegions structure requires the regions to be defined in start address
\r
333 order, so this just creates one big array, then populates the structure with
\r
334 offsets into the array - with gaps in between and messy alignment just for test
\r
336 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
337 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */
\r
338 const HeapRegion_t xHeapRegions[] =
\r
340 /* Start address with dummy offsets Size */
\r
341 { ucHeap + 1, mainREGION_1_SIZE },
\r
342 { ucHeap + 15 + mainREGION_1_SIZE, mainREGION_2_SIZE },
\r
343 { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE, mainREGION_3_SIZE },
\r
347 /* Sanity check that the sizes and offsets defined actually fit into the
\r
349 configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );
\r
351 /* Prevent compiler warnings when configASSERT() is not defined. */
\r
352 ( void ) ulAdditionalOffset;
\r
354 vPortDefineHeapRegions( xHeapRegions );
\r
356 /*-----------------------------------------------------------*/
\r
358 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
\r
359 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
\r
360 used by the Idle task. */
\r
361 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
\r
363 /* If the buffers to be provided to the Idle task are declared inside this
\r
364 function then they must be declared static - otherwise they will be allocated on
\r
365 the stack and so not exists after this function exits. */
\r
366 static StaticTask_t xIdleTaskTCB;
\r
367 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
\r
369 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
\r
370 state will be stored. */
\r
371 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
\r
373 /* Pass out the array that will be used as the Idle task's stack. */
\r
374 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
\r
376 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
\r
377 Note that, as the array is necessarily of type StackType_t,
\r
378 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
379 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
\r
381 /*-----------------------------------------------------------*/
\r
383 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
\r
384 application must provide an implementation of vApplicationGetTimerTaskMemory()
\r
385 to provide the memory that is used by the Timer service task. */
\r
386 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
\r
388 /* If the buffers to be provided to the Timer task are declared inside this
\r
389 function then they must be declared static - otherwise they will be allocated on
\r
390 the stack and so not exists after this function exits. */
\r
391 static StaticTask_t xTimerTaskTCB;
\r
393 /* Pass out a pointer to the StaticTask_t structure in which the Timer
\r
394 task's state will be stored. */
\r
395 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
\r
397 /* Pass out the array that will be used as the Timer task's stack. */
\r
398 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
\r
400 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
\r
401 Note that, as the array is necessarily of type StackType_t,
\r
402 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
403 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
\r