]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/WIN32-MSVC/main.c
Add vTaskGetTaskInfo() function that allows a TaskStatus_t structure to be returned...
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main.c
index fea21698ed92ea9b63b6eb93208d02bfc2ecc167..44a935539277c97434e80dff9065e6b08c05f89d 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-    FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.\r
+    FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
     All rights reserved\r
 \r
     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
@@ -8,14 +8,14 @@
 \r
     FreeRTOS is free software; you can redistribute it and/or modify it under\r
     the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
+    Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
 \r
-       ***************************************************************************\r
+    ***************************************************************************\r
     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
     >>!   obliged to provide the source code for proprietary components     !<<\r
     >>!   outside of the FreeRTOS kernel.                                   !<<\r
-       ***************************************************************************\r
+    ***************************************************************************\r
 \r
     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
     ***************************************************************************\r
 \r
     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
-       the FAQ page "My application does not run, what could be wrong?".  Have you\r
-       defined configASSERT()?\r
+    the FAQ page "My application does not run, what could be wrong?".  Have you\r
+    defined configASSERT()?\r
 \r
-       http://www.FreeRTOS.org/support - In return for receiving this top quality\r
-       embedded software for free we request you assist our global community by\r
-       participating in the support forum.\r
+    http://www.FreeRTOS.org/support - In return for receiving this top quality\r
+    embedded software for free we request you assist our global community by\r
+    participating in the support forum.\r
 \r
-       http://www.FreeRTOS.org/training - Investing in training allows your team to\r
-       be as productive as possible as early as possible.  Now you can receive\r
-       FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
-       Ltd, and the world's leading authority on the world's leading RTOS.\r
+    http://www.FreeRTOS.org/training - Investing in training allows your team to\r
+    be as productive as possible as early as possible.  Now you can receive\r
+    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
+    Ltd, and the world's leading authority on the world's leading RTOS.\r
 \r
     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
  * hardware setup and FreeRTOS hook functions.\r
  *\r
  *******************************************************************************\r
- * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
- * expect to get real time behaviour from the Win32 port or this demo\r
- * application.  It is provided as a convenient development and demonstration\r
- * test bed only.  This was tested using Windows XP on a dual core laptop.\r
- *\r
- * Windows will not be running the FreeRTOS simulator threads continuously, so\r
- * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
- * See the documentation page for the Windows simulator for an explanation of\r
- * the slow timing:\r
+ * NOTE: Windows will not be running the FreeRTOS demo threads continuously, so\r
+ * do not expect to get real time behaviour from the FreeRTOS Windows port, or\r
+ * this demo application.  Also, the timing information in the FreeRTOS+Trace\r
+ * logs have no meaningful units.  See the documentation page for the Windows\r
+ * port for further information:\r
  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
- * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
- *******************************************************************************\r
  *\r
+ * This demo was created using Windows XP on a dual core laptop, and has since\r
+ * been maintained using Windows 7 on a quad core laptop.\r
+ *\r
+ *******************************************************************************\r
  */\r
 \r
 /* Standard includes. */\r
 #include "FreeRTOS.h"\r
 #include "task.h"\r
 \r
-/* This project provides two demo applications.  A simple blinky style project,\r
-and a more comprehensive test and demo application.  The\r
+/* This project provides two demo applications.  A simple blinky style demo\r
+application, and a more comprehensive test and demo application.  The\r
 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.\r
-The simply blinky demo is implemented and described in main_blinky.c.  The more\r
-comprehensive test and demo application is implemented and described in\r
-main_full.c. */\r
-#define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY     0\r
+\r
+If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is 1 then the blinky demo will be built.\r
+The blinky demo is implemented and described in main_blinky.c.\r
+\r
+If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not 1 then the comprehensive test and\r
+demo application will be built.  The comprehensive test and demo application is\r
+implemented and described in main_full.c. */\r
+#define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY     1\r
 \r
 /* This demo uses heap_5.c, and these constants define the sizes of the regions\r
-that make up the total heap.  This is only done to provide an example of heap_5\r
-being used as this demo could easily create one large heap region instead of\r
-multiple smaller heap regions - in which case heap_4.c would be the more\r
-appropriate choice. */\r
+that make up the total heap.  heap_5 is only used for test and example purposes\r
+as this demo could easily create one large heap region instead of multiple\r
+smaller heap regions - in which case heap_4.c would be the more appropriate\r
+choice.  See http://www.freertos.org/a00111.html for an explanation. */\r
 #define mainREGION_1_SIZE      4001\r
 #define mainREGION_2_SIZE      18105\r
 #define mainREGION_3_SIZE      1807\r
 \r
+/*-----------------------------------------------------------*/\r
+\r
 /*\r
  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
@@ -128,31 +132,30 @@ extern void main_blinky( void );
 extern void main_full( void );\r
 \r
 /*\r
- * Some of the RTOS hook (callback) functions only need special processing when\r
- * the full demo is being used.  The simply blinky demo has no special\r
- * requirements, so these functions are called from the hook functions defined\r
- * in this file, but are defined in main_full.c.\r
+ * Only the comprehensive demo uses application hook (callback) functions.  See\r
+ * http://www.freertos.org/a00016.html for more information.\r
  */\r
 void vFullDemoTickHookFunction( void );\r
 void vFullDemoIdleFunction( void );\r
 \r
 /*\r
- * This demo uses heap_5.c, so start by defining some heap regions.  This is\r
- * only done to provide an example as this demo could easily create one large\r
- * heap region instead of multiple smaller heap regions - in which case heap_4.c\r
- * would be the more appropriate choice.  No initialisation is required when\r
- * heap_4.c is used.\r
+ * This demo uses heap_5.c, so start by defining some heap regions.  It is not\r
+ * necessary for this demo to use heap_5, as it could define one large heap\r
+ * region.  Heap_5 is only used for test and example purposes.  See\r
+ * http://www.freertos.org/a00111.html for an explanation.\r
  */\r
 static void  prvInitialiseHeap( void );\r
 \r
 /*\r
- * Prototypes for the standard FreeRTOS callback/hook functions implemented\r
- * within this file.\r
+ * Prototypes for the standard FreeRTOS application hook (callback) functions\r
+ * implemented within this file.  See http://www.freertos.org/a00016.html .\r
  */\r
 void vApplicationMallocFailedHook( void );\r
 void vApplicationIdleHook( void );\r
 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
 void vApplicationTickHook( void );\r
+void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint16_t *pusIdleTaskStackSize );\r
+void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint16_t *pusTimerTaskStackSize );\r
 \r
 /*\r
  * Writes trace data to a disk file when the trace recording is stopped.\r
@@ -160,10 +163,19 @@ void vApplicationTickHook( void );
  */\r
 static void prvSaveTraceFile( void );\r
 \r
+/*-----------------------------------------------------------*/\r
+\r
+/* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can\r
+use a callback function to optionally provide the memory required by the idle\r
+and timer tasks.  This is the stack that will be used by the timer task.  It is\r
+declared here, as a global, so it can be checked by a test that is implemented\r
+in a different file. */\r
+StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
+\r
 /* The user trace event posted to the trace recording on each tick interrupt.\r
-Note tick events will not appear in the trace recording with regular period\r
-because this project runs in a Windows simulator, and does not therefore\r
-exhibit deterministic behaviour. */\r
+Note:  This project runs under Windows, and Windows will not be executing the\r
+RTOS threads continuously.  Therefore tick events will not appear with a regular\r
+interval within the the trace recording. */\r
 traceLabel xTickTraceUserEvent;\r
 static portBASE_TYPE xTraceRunning = pdTRUE;\r
 \r
@@ -171,23 +183,18 @@ static portBASE_TYPE xTraceRunning = pdTRUE;
 \r
 int main( void )\r
 {\r
-       /* This demo uses heap_5.c, so start by defining some heap regions.  This\r
-       is only done to provide an example as this demo could easily create one\r
-       large heap region instead of multiple smaller heap regions - in which case\r
-       heap_4.c would be the more appropriate choice.  No initialisation is\r
-       required when heap_4.c is used. */\r
+       /* This demo uses heap_5.c, so start by defining some heap regions.  heap_5\r
+       is only used for test and example reasons.  Heap_4 is more appropriate.  See\r
+       http://www.freertos.org/a00111.html for an explanation. */\r
        prvInitialiseHeap();\r
 \r
        /* Initialise the trace recorder and create the label used to post user\r
-       events to the trace recording on each tick interrupt. */\r
+       events to the trace recording on each tick interrupt.  Use of the trace\r
+       recorder is optional.  See http://www.FreeRTOS.org/trace for more\r
+       information. */\r
        vTraceInitTraceData();\r
        xTickTraceUserEvent = xTraceOpenLabel( "tick" );\r
 \r
-       /* Start the trace recording - the recording is written to a file if\r
-       configASSERT() is called. */\r
-       printf( "\r\nTrace started.  Hit a key to dump trace file to disk.\r\n" );\r
-       uiTraceStart();\r
-\r
        /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
        of this file. */\r
        #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
@@ -196,6 +203,12 @@ int main( void )
        }\r
        #else\r
        {\r
+               /* Start the trace recording - the recording is written to a file if\r
+               configASSERT() is called. */\r
+               printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );\r
+               printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );\r
+               uiTraceStart();\r
+\r
                main_full();\r
        }\r
        #endif\r
@@ -211,11 +224,13 @@ void vApplicationMallocFailedHook( void )
        function that will get called if a call to pvPortMalloc() fails.\r
        pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
        timer or semaphore is created.  It is also called by various parts of the\r
-       demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
-       heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
-       FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
-       to query the size of free heap space that remains (although it does not\r
-       provide information on how the remaining heap might be fragmented). */\r
+       demo application.  If heap_1.c, heap_2.c or heap_4.c is being used, then the\r
+       size of the     heap available to pvPortMalloc() is defined by\r
+       configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
+       API function can be used to query the size of free heap space that remains\r
+       (although it does not provide information on how the remaining heap might be\r
+       fragmented).  See http://www.freertos.org/a00111.html for more\r
+       information. */\r
        vAssertCalled( __LINE__, __FILE__ );\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -226,11 +241,11 @@ void vApplicationIdleHook( void )
        to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
        task.  It is essential that code added to this hook function never attempts\r
        to block in any way (for example, call xQueueReceive() with a block time\r
-       specified, or call vTaskDelay()).  If the application makes use of the\r
-       vTaskDelete() API function (as this demo application does) then it is also\r
-       important that vApplicationIdleHook() is permitted to return to its calling\r
-       function, because it is the responsibility of the idle task to clean up\r
-       memory allocated by the kernel to any task that has since been deleted. */\r
+       specified, or call vTaskDelay()).  If application tasks make use of the\r
+       vTaskDelete() API function to delete themselves then it is also important\r
+       that vApplicationIdleHook() is permitted to return to its calling function,\r
+       because it is the responsibility of the idle task to clean up memory\r
+       allocated by the kernel to any task that has since deleted itself. */\r
 \r
        /* Uncomment the following code to allow the trace to be stopped with any\r
        key press.  The code is commented out by default as the kbhit() function\r
@@ -264,7 +279,9 @@ void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
 \r
        /* Run time stack overflow checking is performed if\r
        configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
-       function is called if a stack overflow is detected. */\r
+       function is called if a stack overflow is detected.  This function is\r
+       provided as an example only as stack overflow checking does not function\r
+       when running the FreeRTOS Windows port. */\r
        vAssertCalled( __LINE__, __FILE__ );\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -283,24 +300,37 @@ void vApplicationTickHook( void )
        }\r
        #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
 \r
-       /* Write a user event to the trace log.\r
-       Note tick events will not appear in the trace recording with regular period\r
-       because this project runs in a Windows simulator, and does not therefore\r
-       exhibit deterministic behaviour.  Windows will run the simulator in\r
-       bursts. */\r
+       /* Write a user event to the trace log.  Note:  This project runs under\r
+       Windows, and Windows will not be executing the RTOS threads continuously.\r
+       Therefore tick events will not appear with a regular interval within the the\r
+       trace recording. */\r
        vTraceUserEvent( xTickTraceUserEvent );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+void vApplicationDaemonTaskStartupHook( void )\r
+{\r
+       /* This function will be called once only, when the daemon task starts to\r
+       execute (sometimes called the timer task).  This is useful if the\r
+       application includes initialisation code that would benefit from executing\r
+       after the scheduler has been started. */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
 {\r
 static portBASE_TYPE xPrinted = pdFALSE;\r
 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
 \r
+       /* Called if an assertion passed to configASSERT() fails.  See\r
+       http://www.freertos.org/a00110.html#configASSERT for more information. */\r
+\r
        /* Parameters are not used. */\r
        ( void ) ulLine;\r
        ( void ) pcFileName;\r
 \r
+       printf( "ASSERT! Line %d, file %s\r\n", ulLine, pcFileName );\r
+\r
        taskENTER_CRITICAL();\r
        {\r
                /* Stop the trace recording. */\r
@@ -348,13 +378,15 @@ FILE* pxOutputFile;
 \r
 static void  prvInitialiseHeap( void )\r
 {\r
-/* This demo uses heap_5.c, so start by defining some heap regions.  This is\r
-only done to provide an example as this demo could easily create one large heap\r
-region instead of multiple smaller heap regions - in which case heap_4.c would\r
-be the more appropriate choice.  No initialisation is required when heap_4.c is\r
-used.  The xHeapRegions structure requires the regions to be defined in order,\r
-so this just creates one big array, then populates the structure with offsets\r
-into the array - with gaps in between and messy alignment just for test\r
+/* The Windows demo could create one large heap region, in which case it would\r
+be appropriate to use heap_4.  However, purely for demonstration purposes,\r
+heap_5 is used instead, so start by defining some heap regions.  No\r
+initialisation is required when any other heap implementation is used.  See\r
+http://www.freertos.org/a00111.html for more information.\r
+\r
+The xHeapRegions structure requires the regions to be defined in start address\r
+order, so this just creates one big array, then populates the structure with\r
+offsets into the array - with gaps in between and messy alignment just for test\r
 purposes. */\r
 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */\r
@@ -378,3 +410,38 @@ const HeapRegion_t xHeapRegions[] =
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint16_t *pusIdleTaskStackSize )\r
+{\r
+/* The buffers used by the idle task must be static so they are persistent, and\r
+so exist after this function returns. */\r
+static StaticTask_t xIdleTaskTCB;\r
+static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
+\r
+       /* configUSE_STATIC_ALLOCATION is set to 1, so the application has the\r
+       opportunity to supply the buffers that will be used by the Idle task as its\r
+       stack and to hold its TCB.  If these are set to NULL then the buffers will\r
+       be allocated dynamically, just as if xTaskCreate() had been called. */\r
+       *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
+       *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
+       *pusIdleTaskStackSize = configMINIMAL_STACK_SIZE; /* In words.  NOT in bytes! */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint16_t *pusTimerTaskStackSize )\r
+{\r
+/* The buffers used by the Timer/Daemon task must be static so they are\r
+persistent, and so exist after this function returns.  The stack buffer is\r
+not declared here, but globally, as it is checked by a test in a different\r
+file. */\r
+static StaticTask_t xTimerTaskTCB;\r
+\r
+       /* configUSE_STATIC_ALLOCATION is set to 1, so the application has the\r
+       opportunity to supply the buffers that will be used by the Timer/RTOS daemon\r
+       task as its     stack and to hold its TCB.  If these are set to NULL then the\r
+       buffers will be allocated dynamically, just as if xTaskCreate() had been\r
+       called. */\r
+       *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
+       *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
+       *pusTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; /* In words.  NOT in bytes! */\r
+}\r
+\r