]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Posix_GCC/src/main.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / Posix_GCC / src / main.c
1 /*
2  * FreeRTOS Kernel V10.3.0
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
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
34  * main_full.c.
35  *
36  * This file implements the code that is not demo specific, including the
37  * hardware setup and FreeRTOS hook functions.
38  *
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
46  *
47
48  *
49  *******************************************************************************
50  */
51
52 /* Standard includes. */
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <unistd.h>
56
57 /* FreeRTOS kernel includes. */
58 #include "FreeRTOS.h"
59 #include "task.h"
60
61 /* Local includes. */
62 #include "console.h"
63
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.
67
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.
70
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
76 #endif
77
78 /* This demo uses heap_3.c (the libc provided malloc() and free()). */
79
80 /*-----------------------------------------------------------*/
81
82 /*
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.
85  */
86 extern void main_blinky( void );
87 extern void main_full( void );
88
89 /*
90  * Only the comprehensive demo uses application hook (callback) functions.  See
91  * http://www.freertos.org/a00016.html for more information.
92  */
93 void vFullDemoTickHookFunction( void );
94 void vFullDemoIdleFunction( void );
95
96 /*
97  * Prototypes for the standard FreeRTOS application hook (callback) functions
98  * implemented within this file.  See http://www.freertos.org/a00016.html .
99  */
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 );
106
107 /*
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.
110  */
111 static void prvSaveTraceFile( void );
112
113 /*-----------------------------------------------------------*/
114
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 ];
121
122 /* Notes if the trace is running or not. */
123 static BaseType_t xTraceRunning = pdTRUE;
124
125 /*-----------------------------------------------------------*/
126
127 int main( void )
128 {
129         /* Do not include trace code when performing a code coverage analysis. */
130         #if( projCOVERAGE_TEST != 1 )
131         {
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 );
135
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" );
140                 uiTraceStart();
141         }
142         #endif
143
144         console_init();
145
146         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
147         of this file. */
148         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
149         {
150                 main_blinky();
151         }
152         #else
153         {
154                 main_full();
155         }
156         #endif
157
158         return 0;
159 }
160 /*-----------------------------------------------------------*/
161
162 void vApplicationMallocFailedHook( void )
163 {
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
175         information. */
176         vAssertCalled( __LINE__, __FILE__ );
177 }
178 /*-----------------------------------------------------------*/
179
180 void vApplicationIdleHook( void )
181 {
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. */
191
192         sleep(1);
193
194         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
195         {
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();
199         }
200         #endif
201 }
202 /*-----------------------------------------------------------*/
203
204 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
205 {
206         ( void ) pcTaskName;
207         ( void ) pxTask;
208
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__ );
215 }
216 /*-----------------------------------------------------------*/
217
218 void vApplicationTickHook( void )
219 {
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()). */
225
226         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
227         {
228                 vFullDemoTickHookFunction();
229         }
230         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
231 }
232 /*-----------------------------------------------------------*/
233
234 void vApplicationDaemonTaskStartupHook( void )
235 {
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. */
240 }
241 /*-----------------------------------------------------------*/
242
243 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
244 {
245 static BaseType_t xPrinted = pdFALSE;
246 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
247
248         /* Called if an assertion passed to configASSERT() fails.  See
249         http://www.freertos.org/a00110.html#configASSERT for more information. */
250
251         /* Parameters are not used. */
252         ( void ) ulLine;
253         ( void ) pcFileName;
254
255
256         taskENTER_CRITICAL();
257         {
258                 /* Stop the trace recording. */
259                 if( xPrinted == pdFALSE )
260                 {
261                         xPrinted = pdTRUE;
262                         if( xTraceRunning == pdTRUE )
263                         {
264                                 prvSaveTraceFile();
265                         }
266                 }
267
268                 /* You can step out of this function to debug the assertion by using
269                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero
270                 value. */
271                 while( ulSetToNonZeroInDebuggerToContinue == 0 )
272                 {
273                         __asm volatile( "NOP" );
274                         __asm volatile( "NOP" );
275                 }
276         }
277         taskEXIT_CRITICAL();
278 }
279 /*-----------------------------------------------------------*/
280
281 static void prvSaveTraceFile( void )
282 {
283         /* Tracing is not used when code coverage analysis is being performed. */
284         #if( projCOVERAGE_TEST != 1 )
285         {
286                 FILE* pxOutputFile;
287
288                 vTraceStop();
289
290                 pxOutputFile = fopen( "Trace.dump", "wb");
291
292                 if( pxOutputFile != NULL )
293                 {
294                         fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
295                         fclose( pxOutputFile );
296                         printf( "\r\nTrace output saved to Trace.dump\r\n" );
297                 }
298                 else
299                 {
300                         printf( "\r\nFailed to create trace dump file\r\n" );
301                 }
302         }
303         #endif
304 }
305 /*-----------------------------------------------------------*/
306
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 )
311 {
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 ];
317
318         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
319         state will be stored. */
320         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
321
322         /* Pass out the array that will be used as the Idle task's stack. */
323         *ppxIdleTaskStackBuffer = uxIdleTaskStack;
324
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;
329 }
330 /*-----------------------------------------------------------*/
331
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 )
336 {
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;
341
342         /* Pass out a pointer to the StaticTask_t structure in which the Timer
343         task's state will be stored. */
344         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
345
346         /* Pass out the array that will be used as the Timer task's stack. */
347         *ppxTimerTaskStackBuffer = uxTimerTaskStack;
348
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;
353 }
354