]> git.sur5r.net Git - freertos/blobdiff - Demo/WIN32-MSVC/main.c
Add a few lines to the RX ports that allow the vector to be installed when the FreeRT...
[freertos] / Demo / WIN32-MSVC / main.c
index b5b7d497bfb5ebacb07cca44b97eb6032dd917a0..f067143e18b250a23f0987c96c0077060d2db6fb 100644 (file)
@@ -1,38 +1,38 @@
 /*\r
-    FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
+    FreeRTOS V7.1.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
+       \r
 \r
     ***************************************************************************\r
-    *                                                                         *\r
-    * If you are:                                                             *\r
-    *                                                                         *\r
-    *    + New to FreeRTOS,                                                   *\r
-    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
-    *    + Looking for basic training,                                        *\r
-    *    + Wanting to improve your FreeRTOS skills and productivity           *\r
-    *                                                                         *\r
-    * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
-    *                                                                         *\r
-    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
-    *                  http://www.FreeRTOS.org/Documentation                  *\r
-    *                                                                         *\r
-    * A pdf reference manual is also available.  Both are usually delivered   *\r
-    * to your inbox within 20 minutes to two hours when purchased between 8am *\r
-    * and 8pm GMT (although please allow up to 24 hours in case of            *\r
-    * exceptional circumstances).  Thank you for your support!                *\r
-    *                                                                         *\r
+     *                                                                       *\r
+     *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
+     *    Complete, revised, and edited pdf reference manuals are also       *\r
+     *    available.                                                         *\r
+     *                                                                       *\r
+     *    Purchasing FreeRTOS documentation will not only help you, by       *\r
+     *    ensuring you get running as quickly as possible and with an        *\r
+     *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
+     *    the FreeRTOS project to continue with its mission of providing     *\r
+     *    professional grade, cross platform, de facto standard solutions    *\r
+     *    for microcontrollers - completely free of charge!                  *\r
+     *                                                                       *\r
+     *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
+     *                                                                       *\r
+     *    Thank you for using FreeRTOS, and thank you for your support!      *\r
+     *                                                                       *\r
     ***************************************************************************\r
 \r
+\r
     This file is part of the FreeRTOS distribution.\r
 \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
-    ***NOTE*** The exception to the GPL is included to allow you to distribute\r
-    a combined work that includes FreeRTOS without being obliged to provide the\r
-    source code for proprietary components outside of the FreeRTOS kernel.\r
-    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
-    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
-    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+    >>>NOTE<<< The modification to the GPL is included to allow you to\r
+    distribute a combined work that includes FreeRTOS without being obliged to\r
+    provide the source code for proprietary components outside of the FreeRTOS\r
+    kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
+    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
     more details. You should have received a copy of the GNU General Public\r
     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
     licensing and training services.\r
 */\r
 \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
+ * - 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
+\r
+/* Kernel includes. */\r
 #include <FreeRTOS.h>\r
 #include "task.h"\r
 #include "queue.h"\r
-\r
-/* Task priorities. */\r
-#define mainSTDOUT_TASK_PRIORITY       tskIDLE_PRIORITY\r
-\r
-/* Stack sizes. */\r
-#define mainSTDOUT_TASK_STACK_SIZE             ( configMINIMAL_STACK_SIZE * 4 )\r
-\r
-/* File scope variables. */\r
-static volatile unsigned long ul1 = 0, ul2 = 0;\r
-static xQueueHandle xStdoutQueue = NULL;\r
+#include "timers.h"\r
+#include "semphr.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
+#include "TimerDemo.h"\r
+#include "countsem.h"\r
+#include "death.h"\r
+#include "dynamic.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
+#define mainTIMER_TEST_PERIOD                  ( 50 )\r
 \r
 /* Task function prototypes. */\r
-static void prvTask1( void *pvParameters );\r
-static void prvTask2( void *pvParameters );\r
-static void prvStdoutTask( void *pvParameters );\r
+static void prvCheckTask( void *pvParameters );\r
 \r
-/* Create a queue on which console output strings can be posted, then start the\r
-task that processes the queue - printf()'ing each string that is received. */\r
-static void prvStartStdoutTask( void );\r
+/* The variable into which error messages are latched. */\r
+static char *pcStatusMessage = "OK";\r
 \r
-/* Post a message for output by the stdout task.  Basically queues the message\r
-pointed to by pcTextToPrint for output to stdout in a thread safe manner. */\r
-void vMainConsolePrint( const char *pcTextToPrint, portTickType xTicksToWait );\r
+/* This semaphore is created purely to test using the vSemaphoreDelete() and\r
+semaphore tracing API functions.  It has no other purpose. */\r
+static xSemaphoreHandle xMutexToDelete = NULL;\r
 \r
-volatile unsigned long ulIdleCount = 0UL, ulT1Count = 0UL, ulT2Count = 0UL, ulTicks = 0UL;\r
 /*-----------------------------------------------------------*/\r
 \r
 int main( void )\r
 {\r
-       prvStartStdoutTask();\r
-       xTaskCreate( prvTask1, "t1", 100, NULL, 0, NULL );\r
-       xTaskCreate( prvTask2, "t2", 100, NULL, 0, NULL );\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
+       vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
+       vStartCountingSemaphoreTasks();\r
+       vStartDynamicPriorityTasks();\r
+\r
+       /* The suicide tasks must be created last as they need to know how many\r
+       tasks were running prior to their creation.  This then allows them to \r
+       ascertain whether or not the correct/expected number of tasks are running at \r
+       any given time. */\r
+       vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
+\r
+       /* Create the semaphore that will be deleted in the idle task hook.  This\r
+       is done purely to test the use of vSemaphoreDelete(). */\r
+       xMutexToDelete = xSemaphoreCreateMutex();\r
+\r
+       /* Start the scheduler itself. */\r
        vTaskStartScheduler();\r
 \r
     /* Should never get here unless there was not enough heap space to create \r
@@ -95,102 +166,167 @@ int main( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vMainConsolePrint( const char *pcTextToPrint, portTickType xTicksToWait )\r
+static void prvCheckTask( void *pvParameters )\r
 {\r
-       if( xStdoutQueue != NULL )\r
-       {\r
-               xQueueSend( xStdoutQueue, &pcTextToPrint, xTicksToWait );\r
-       }\r
-}\r
-/*-----------------------------------------------------------*/\r
+portTickType xNextWakeTime;\r
+const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;\r
 \r
-static void prvStartStdoutTask( void )\r
-{\r
-const unsigned long ulQueueLength = 20;\r
+       /* Just to remove compiler warning. */\r
+       ( void ) pvParameters;\r
 \r
-       /* Create the queue on which starings for output will be stored. */\r
-       xStdoutQueue = xQueueCreate( ulQueueLength, ( unsigned portBASE_TYPE ) sizeof( char * ) );\r
+       /* Initialise xNextWakeTime - this only needs to be done once. */\r
+       xNextWakeTime = xTaskGetTickCount();\r
 \r
-       if( xStdoutQueue != NULL )\r
+       for( ;; )\r
        {\r
-               /* Create the task that processes the stdout messages. */\r
-               xTaskCreate( prvStdoutTask, "stdout task", mainSTDOUT_TASK_STACK_SIZE, NULL, mainSTDOUT_TASK_PRIORITY, NULL );\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( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )\r
+               {\r
+                       pcStatusMessage = "Error: TimerDemo";\r
+               }\r
+           else 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
+               {\r
+                       pcStatusMessage = "Error: Flop";\r
+               }\r
+           else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
+           {\r
+                       pcStatusMessage = "Error: RecMutex";\r
+               }\r
+               else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
+               {\r
+                       pcStatusMessage = "Error: CountSem";\r
+               }\r
+               else if( xIsCreateTaskStillRunning() != pdTRUE )\r
+               {\r
+                       pcStatusMessage = "Error: Death";\r
+               }\r
+               else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
+               {\r
+                       pcStatusMessage = "Error: Dynamic\r\n";\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, xTaskGetTickCount() );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvStdoutTask( void *pvParameters )\r
+void vApplicationIdleHook( void )\r
 {\r
-char *pcString;\r
+const unsigned long ulMSToSleep = 5;\r
+xTaskHandle xIdleTaskHandle, xTimerTaskHandle;\r
+signed char *pcTaskName;\r
+const unsigned char ucConstQueueNumber = 0xaaU, ucConstTaskNumber = 0x55U;\r
+\r
+/* These three functions are only meant for use by trace code, and not for\r
+direct use from application code, hence their prototypes are not in queue.h. */\r
+extern void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber );\r
+extern unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue );\r
+extern unsigned char ucQueueGetQueueType( xQueueHandle pxQueue );\r
+extern void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );\r
+extern unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );\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
+\r
+       /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and \r
+       xTaskGetIdleTaskHandle() functions.  Also try using the function that sets\r
+       the task number. */\r
+       xIdleTaskHandle = xTaskGetIdleTaskHandle();\r
+       xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();\r
+       vTaskSetTaskNumber( xIdleTaskHandle, ( unsigned long ) ucConstTaskNumber );\r
+       configASSERT( uxTaskGetTaskNumber( xIdleTaskHandle ) == ucConstTaskNumber );\r
+\r
+       /* This is the idle hook, so the current task handle should equal the \r
+       returned idle task handle. */\r
+       if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )\r
+       {\r
+               pcStatusMessage = "Error:  Returned idle task handle was incorrect";\r
+       }\r
 \r
-       /* Just to remove compiler warnings. */\r
-       ( void ) pvParameters;\r
+       /* Check the timer task handle was returned correctly. */\r
+       pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );\r
+       if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )\r
+       {\r
+               pcStatusMessage = "Error:  Returned timer task handle was incorrect";\r
+       }\r
 \r
-       for( ;; )\r
+       /* If xMutexToDelete has not already been deleted, then delete it now.\r
+       This is done purely to demonstrate the use of, and test, the \r
+       vSemaphoreDelete() macro.  Care must be taken not to delete a semaphore\r
+       that has tasks blocked on it. */\r
+       if( xMutexToDelete != NULL )\r
        {\r
-               /* This task would not have been created if the queue had not been created\r
-               successfully too.  Also, because of the FreeRTOSConfig.h settings using\r
-               portMAX_DELAY in this case means wait forever, so when this function returns\r
-               we know there is a string to print. */\r
-               xQueueReceive( xStdoutQueue, &pcString, portMAX_DELAY );\r
-               printf( "%s", pcString );\r
-               //fflush( stdout );\r
+               /* Before deleting the semaphore, test the function used to set its\r
+               number.  This would normally only be done from trace software, rather\r
+               than application code. */\r
+               vQueueSetQueueNumber( xMutexToDelete, ucConstQueueNumber );\r
+\r
+               /* Before deleting the semaphore, test the functions used to get its\r
+               type and number.  Again, these would normally only be done from trace\r
+               software, rather than application code. */\r
+               configASSERT( ucQueueGetQueueNumber( xMutexToDelete ) == ucConstQueueNumber );\r
+               configASSERT( ucQueueGetQueueType( xMutexToDelete ) == queueQUEUE_TYPE_MUTEX );\r
+               vSemaphoreDelete( xMutexToDelete );\r
+               xMutexToDelete = NULL;\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvTask1( void *pvParameters )\r
+void vApplicationMallocFailedHook( void )\r
 {\r
-const char *pcTask1Message = "Task 1 running\r\n";\r
-const portTickType xTicksToDelay = 1000 / portTICK_RATE_MS;\r
-\r
-       /* Just to remove compiler warnings. */\r
-       ( void ) pvParameters;\r
-\r
-       for( ;; )\r
-       {\r
-//             ul1++;\r
-               vMainConsolePrint( pcTask1Message, 0 );\r
-               vTaskDelay( xTicksToDelay );\r
-               ulT1Count++;\r
-       }\r
+       /* Can be implemented if required, but probably not required in this \r
+       environment and running this demo. */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvTask2( void *pvParameters )\r
+void vApplicationStackOverflowHook( void )\r
 {\r
-const char *pcTask2Message = "Task 2 running\r\n";\r
-const portTickType xTicksToDelay = 500 / portTICK_RATE_MS;\r
-\r
-       /* Just to remove compiler warnings. */\r
-       ( void ) pvParameters;\r
-\r
-       for( ;; )\r
-       {\r
-//             ul2++;\r
-               vMainConsolePrint( pcTask2Message, 0 );\r
-               vTaskDelay( xTicksToDelay );\r
-               ulT2Count++;\r
-//             taskYIELD();\r
-       }\r
+       /* Can be implemented if required, but not required in this \r
+       environment and running this demo. */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vApplicationIdleHook()\r
+void vApplicationTickHook( void )\r
 {\r
-const unsigned long ulMSToSleep = 5;\r
-\r
-       /* Sleep to reduce CPU load, but don't sleep indefinitely if not using \r
-       preemption as as nothing will cause     a task switch. */\r
-       #if configUSE_PREEMPTION != 0\r
-       {\r
-               SleepEx( INFINITE, TRUE );\r
-       }\r
-       #else\r
-       {\r
-               SleepEx( ulMSToSleep, TRUE );\r
-       }\r
-       #endif\r
+       /* Call the periodic timer test, which tests the timer API functions that\r
+       can be called from an ISR. */\r
+       vTimerPeriodicISRTests();\r
+}\r
+/*-----------------------------------------------------------*/\r
 \r
-       ulIdleCount++;\r
+void vAssertCalled( void )\r
+{\r
+       taskDISABLE_INTERRUPTS();\r
+       for( ;; );\r
 }\r
+\r