]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/WIN32-MingW/main.c
Update the demo directory to use the version 8 type naming conventions.
[freertos] / FreeRTOS / Demo / WIN32-MingW / main.c
index 0d50610f23de90ef7fd36d189b480c83a7b79e89..c607bb9b57022a70b7e50b7b1a38f42ea06198aa 100644 (file)
@@ -1,5 +1,6 @@
 /*\r
-    FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
+    FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd. \r
+    All rights reserved\r
 \r
     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
 \r
     1 tab == 4 spaces!\r
 */\r
 \r
-/*\r
+/******************************************************************************\r
+ * This project provides two demo applications.  A simple blinky style project,\r
+ * 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 \r
+ * more comprehensive test and demo application is implemented and described in \r
+ * main_full.c.\r
+ *\r
+ * This file implements the code that is not demo specific, including the\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
+ * 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
- * main() creates all the demo application tasks, then starts the scheduler.  \r
- * The web documentation provides more details of the standard demo application \r
- * tasks, which provide no particular functionality but do provide a good \r
- * example of how to use the FreeRTOS API.\r
- *\r
- * In addition to the standard demo tasks, the following tasks and tests are\r
- * defined and/or created within this file:\r
- *\r
- * "Check" task - This only executes every five seconds but has a high priority\r
- * to ensure it gets processor time.  Its main function is to check that all the\r
- * standard demo tasks are still operational.  While no errors have been\r
- * discovered the check task will print out "OK" and the current simulated tick\r
- * time.  If an error is discovered in the execution of a task then the check\r
- * task will print out an appropriate error message.\r
- *\r
  */\r
 \r
-\r
 /* Standard includes. */\r
 #include <stdio.h>\r
+#include <stdlib.h>\r
+#include <conio.h>\r
 \r
-/* Kernel includes. */\r
-#include <FreeRTOS.h>\r
+/* FreeRTOS kernel includes. */\r
+#include "FreeRTOS.h"\r
 #include "task.h"\r
-#include "queue.h"\r
-\r
-/* Standard demo includes. */\r
-#include "BlockQ.h"\r
-#include "integer.h"\r
-#include "semtest.h"\r
-#include "PollQ.h"\r
-#include "GenQTest.h"\r
-#include "QPeek.h"\r
-#include "recmutex.h"\r
-#include "flop.h"\r
-\r
-/* Priorities at which the tasks are created. */\r
-#define mainCHECK_TASK_PRIORITY                ( configMAX_PRIORITIES - 1 )\r
-#define mainQUEUE_POLL_PRIORITY                ( tskIDLE_PRIORITY + 1 )\r
-#define mainSEM_TEST_PRIORITY          ( tskIDLE_PRIORITY + 1 )\r
-#define mainBLOCK_Q_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
-#define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
-#define mainFLASH_TASK_PRIORITY                ( tskIDLE_PRIORITY + 1 )\r
-#define mainuIP_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
-#define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
-#define mainGEN_QUEUE_TASK_PRIORITY    ( tskIDLE_PRIORITY )\r
-#define mainFLOP_TASK_PRIORITY         ( tskIDLE_PRIORITY )\r
-\r
-/* Task function prototypes. */\r
-static void prvCheckTask( void *pvParameters );\r
+\r
+/* This project provides two demo applications.  A simple blinky style project,\r
+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     1\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
+ */\r
+extern void main_blinky( void );\r
+extern void main_full( void );\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 requirements,\r
+so these functions are called from the hook functions defined in this file, but\r
+are defined in main_full.c. */\r
+void vFullDemoTickHookFunction( void );\r
+void vFullDemoIdleFunction( void );\r
+\r
+/* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
+within this file. */\r
+void vApplicationMallocFailedHook( void );\r
+void vApplicationIdleHook( void );\r
+void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
+void vApplicationTickHook( void );\r
+\r
+/*\r
+ * Writes trace data to a disk file when the trace recording is stopped.\r
+ * This function will simply overwrite any trace files that already exist.\r
+ */\r
+static void prvSaveTraceFile( void );\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
+traceLabel xTickTraceUserEvent;\r
+static portBASE_TYPE xTraceRunning = pdTRUE;\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
 int main( void )\r
 {\r
-       /* Start the check task as described at the top of this file. */\r
-       xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
-\r
-       /* Create the standard demo tasks. */\r
-       vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
-       vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
-       vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
-       vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
-       vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
-       vStartQueuePeekTasks();\r
-       vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
-       vStartRecursiveMutexTasks();\r
-\r
-       /* Start the scheduler itself. */\r
-       vTaskStartScheduler();\r
-\r
-    /* Should never get here unless there was not enough heap space to create \r
-       the idle and other system tasks. */\r
-    return 0;\r
+       /* Initialise the trace recorder and create the label used to post user\r
+       events to the trace recording on each tick interrupt. */\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 (does not work from Eclipse console).\r\n" );\r
+       fflush( stdout );\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
+       {\r
+               main_blinky();\r
+       }\r
+       #else\r
+       {\r
+               main_full();\r
+       }\r
+       #endif\r
+\r
+       return 0;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvCheckTask( void *pvParameters )\r
+void vApplicationMallocFailedHook( void )\r
 {\r
-portTickType xNextWakeTime;\r
-const portTickType xCycleFrequency = 5000 / portTICK_RATE_MS;\r
-char *pcStatusMessage = "OK";\r
-\r
-       /* Just to remove compiler warning. */\r
-       ( void ) pvParameters;\r
-\r
-       /* Initialise xNextWakeTime - this only needs to be done once. */\r
-       xNextWakeTime = xTaskGetTickCount();\r
+       /* vApplicationMallocFailedHook() will only be called if\r
+       configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
+       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
+       vAssertCalled( __LINE__, __FILE__ );\r
+}\r
+/*-----------------------------------------------------------*/\r
 \r
-       for( ;; )\r
-       {\r
-               /* Place this task in the blocked state until it is time to run again. */\r
-               vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
-\r
-               /* Check the standard demo tasks are running without error. */\r
-           if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
-           {\r
-                       pcStatusMessage = "Error: IntMath";\r
-           }   \r
-               else if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
-               {                       \r
-                       pcStatusMessage = "Error: GenQueue";\r
-               }\r
-               else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
-               {\r
-                       pcStatusMessage = "Error: QueuePeek";\r
-               }\r
-               else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
-               {\r
-                       pcStatusMessage = "Error: BlockQueue";\r
-               }\r
-           else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
-           {\r
-                       pcStatusMessage = "Error: SemTest";\r
-           }\r
-           else if( xArePollingQueuesStillRunning() != pdTRUE )\r
-           {\r
-                       pcStatusMessage = "Error: PollQueue";\r
-           }\r
-               else if( xAreMathsTaskStillRunning() != pdPASS )\r
+void vApplicationIdleHook( void )\r
+{\r
+       /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
+       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
+\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
+       interferes with the run time behaviour. */\r
+       /* \r
+               if( _kbhit() != pdFALSE )\r
                {\r
-                       pcStatusMessage = "Error: Flop";\r
-               }\r
-           else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
-           {\r
-                       pcStatusMessage = "Error: RecMutex";\r
+                       if( xTraceRunning == pdTRUE )\r
+                       {\r
+                               vTraceStop();\r
+                               prvSaveTraceFile();\r
+                               xTraceRunning = pdFALSE;\r
+                       }\r
                }\r
+       */\r
 \r
-               /* This is the only task that uses stdout so its ok to call printf() \r
-               directly. */\r
-               printf( "%s - %d\r\n", pcStatusMessage, ( int ) xTaskGetTickCount() );\r
-               fflush( stdout ); /* Required by Eclipse console. */\r
+       #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
+       {\r
+               /* Call the idle task processing used by the full demo.  The simple\r
+               blinky demo does not use the idle task hook. */\r
+               vFullDemoIdleFunction();\r
        }\r
+       #endif\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vApplicationIdleHook( void )\r
+void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
 {\r
-const unsigned long ulMSToSleep = 5;\r
+       ( void ) pcTaskName;\r
+       ( void ) pxTask;\r
 \r
-       /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
-       tasks waiting to be terminated by the idle task. */\r
-       Sleep( ulMSToSleep );\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
+       vAssertCalled( __LINE__, __FILE__ );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vApplicationMallocFailedHook( void )\r
+void vApplicationTickHook( void )\r
 {\r
-       /* Can be implemented if required, but probably not required in this \r
-       environment and running this demo. */\r
+       /* This function will be called by each tick interrupt if\r
+       configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
+       added here, but the tick hook is called from an interrupt context, so\r
+       code must not attempt to block, and only the interrupt safe FreeRTOS API\r
+       functions can be used (those that end in FromISR()). */\r
+\r
+       #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
+       {\r
+               vFullDemoTickHookFunction();\r
+       }\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
+       vTraceUserEvent( xTickTraceUserEvent );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vApplicationStackOverflowHook( void )\r
+void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
 {\r
-       /* Can be implemented if required, but not required in this \r
-       environment and running this demo. */\r
+static portBASE_TYPE xPrinted = pdFALSE;\r
+\r
+       /* Parameters are not used. */\r
+       ( void ) ulLine;\r
+       ( void ) pcFileName;\r
+\r
+       taskDISABLE_INTERRUPTS();\r
+       __asm volatile( "int $3" );\r
+\r
+       /* Stop the trace recording. */\r
+       if( xPrinted == pdFALSE )\r
+       {\r
+               xPrinted = pdTRUE;\r
+               if( xTraceRunning == pdTRUE )\r
+               {\r
+               vTraceStop();\r
+               prvSaveTraceFile();\r
+               }\r
+       }\r
+\r
+       taskENABLE_INTERRUPTS();\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSaveTraceFile( void )\r
+{\r
+FILE* pxOutputFile;\r
+\r
+       pxOutputFile = fopen( "Trace.dump", "wb");\r
 \r
+       if( pxOutputFile != NULL )\r
+       {\r
+               fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
+               fclose( pxOutputFile );\r
+               printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
+       }\r
+       else\r
+       {\r
+               printf( "\r\nFailed to create trace dump file\r\n" );\r
+       }\r
+}\r