*/\r
\r
/*\r
- * Creates eight tasks, each of which loops continuously performing an (emulated) \r
- * floating point calculation.\r
+ * Creates eight tasks, each of which loops continuously performing a floating \r
+ * point calculation and in so doing test the floating point context switching.\r
+ * This file also demonstrates the use of the xPortUsesFloatingPoint() function\r
+ * which informs the kernel that the task requires its floating point context\r
+ * saved on each switch.\r
*\r
* All the tasks run at the idle priority and never block or yield. This causes \r
- * all eight tasks to time slice with the idle task. Running at the idle priority \r
- * means that these tasks will get pre-empted any time another task is ready to run\r
- * or a time slice occurs. More often than not the pre-emption will occur mid \r
- * calculation, creating a good test of the schedulers context switch mechanism - a \r
- * calculation producing an unexpected result could be a symptom of a corruption in \r
- * the context of a task.\r
+ * all eight tasks to time slice with the idle task. Running at the idle \r
+ * priority means that these tasks will get pre-empted any time another task is \r
+ * ready to run or a time slice occurs. More often than not the pre-emption \r
+ * will occur mid calculation, creating a good test of the schedulers context \r
+ * switch mechanism - a calculation producing an unexpected result could be a \r
+ * symptom of a corruption in the context of a task.\r
*/\r
\r
#include <stdlib.h>\r
\r
/* Four tasks, each of which performs a different floating point calculation. \r
Each of the four is created twice. */\r
-static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
-static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
-static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
-static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
+static void vCompetingMathTask1( void *pvParameters );\r
+static void vCompetingMathTask2( void *pvParameters );\r
+static void vCompetingMathTask3( void *pvParameters );\r
+static void vCompetingMathTask4( void *pvParameters );\r
\r
/* These variables are used to check that all the tasks are still running. If a \r
-task gets a calculation wrong it will\r
-stop incrementing its check variable. */\r
+task gets a calculation wrong it will stop incrementing its check variable,\r
+otherwise the check variable will get incremented on each iteration of the \r
+tasks execution. */\r
static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
\r
/*-----------------------------------------------------------*/\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
+static void vCompetingMathTask1( void *pvParameters )\r
{\r
volatile double d1, d2, d3, d4;\r
volatile unsigned short *pusTaskCheckVariable;\r
d2 = 2345.6789;\r
d3 = -918.222;\r
\r
+ /* Calculate the expected answer. */\r
dAnswer = ( d1 + d2 ) * d3;\r
\r
/* The variable this task increments to show it is still running is passed in \r
/* Keep performing a calculation and checking the result against a constant. */\r
for(;;)\r
{\r
+ /* Perform the calculation. */\r
d1 = 123.4567;\r
d2 = 2345.6789;\r
d3 = -918.222;\r
\r
d4 = ( d1 + d2 ) * d3;\r
\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
-\r
/* If the calculation does not match the expected constant, stop the \r
increment of the check variable. */\r
if( fabs( d4 - dAnswer ) > 0.001 )\r
variable so we know this task is still running okay. */\r
( *pusTaskCheckVariable )++;\r
}\r
-\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
-\r
}\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
+static void vCompetingMathTask2( void *pvParameters )\r
{\r
volatile double d1, d2, d3, d4;\r
volatile unsigned short *pusTaskCheckVariable;\r
d2 = 32498.2;\r
d3 = -2.0001;\r
\r
+ /* Calculate the expected answer. */\r
dAnswer = ( d1 / d2 ) * d3;\r
\r
\r
/* Keep performing a calculation and checking the result against a constant. */\r
for( ;; )\r
{\r
+ /* Perform the calculation. */\r
d1 = -389.38;\r
d2 = 32498.2;\r
d3 = -2.0001;\r
\r
d4 = ( d1 / d2 ) * d3;\r
\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
- \r
/* If the calculation does not match the expected constant, stop the \r
increment of the check variable. */\r
if( fabs( d4 - dAnswer ) > 0.001 )\r
this task is still running okay. */\r
( *pusTaskCheckVariable )++;\r
}\r
-\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
}\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
+static void vCompetingMathTask3( void *pvParameters )\r
{\r
volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
volatile unsigned short *pusTaskCheckVariable;\r
size_t xPosition;\r
short sError = pdFALSE;\r
\r
- /* The variable this task increments to show it is still running is passed in \r
- as the parameter. */\r
+ /* The variable this task increments to show it is still running is passed \r
+ in as the parameter. */\r
pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
\r
+ /* Allocate memory for use as an array. */\r
pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
\r
- /* Keep filling an array, keeping a running total of the values placed in the \r
- array. Then run through the array adding up all the values. If the two totals \r
- do not match, stop the check variable from incrementing. */\r
+ /* Keep filling an array, keeping a running total of the values placed in \r
+ the array. Then run through the array adding up all the values. If the two \r
+ totals do not match, stop the check variable from incrementing. */\r
for( ;; )\r
{\r
dTotal1 = 0.0;\r
dTotal1 += ( double ) xPosition + 5.5; \r
}\r
\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
-\r
for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
{\r
dTotal2 += pdArray[ xPosition ];\r
sError = pdTRUE;\r
}\r
\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
-\r
if( sError == pdFALSE )\r
{\r
/* If the calculation has always been correct, increment the check \r
}\r
/*-----------------------------------------------------------*/\r
\r
-static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
+static void vCompetingMathTask4( void *pvParameters )\r
{\r
volatile double *pdArray, dTotal1, dTotal2, dDifference;\r
volatile unsigned short *pusTaskCheckVariable;\r
as the parameter. */\r
pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
\r
+ /* Allocate RAM for use as an array. */\r
pdArray = ( double * ) pvPortMalloc( xArraySize * sizeof( double ) );\r
\r
/* Keep filling an array, keeping a running total of the values placed in the \r
dTotal1 += ( double ) xPosition * 12.123; \r
}\r
\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
-\r
for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
{\r
dTotal2 += pdArray[ xPosition ];\r
sError = pdTRUE;\r
}\r
\r
- #if configUSE_PREEMPTION == 0\r
- taskYIELD();\r
- #endif\r
-\r
if( sError == pdFALSE )\r
{\r
/* If the calculation has always been correct, increment the check \r
/* This is called to check that all the created tasks are still running. */\r
portBASE_TYPE xAreMathsTaskStillRunning( void )\r
{\r
-/* Keep a history of the check variables so we know if they have been incremented \r
-since the last call. */\r
+/* Keep a history of the check variables so we know if they have been \r
+incremented since the last call. */\r
static unsigned short usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
portBASE_TYPE xReturn = pdTRUE, xTask;\r
\r
licensing and training services.\r
*/\r
\r
+\r
+\r
+/*\r
+ * Creates all the demo application tasks, then starts the scheduler. The WEB\r
+ * documentation provides more details of the standard demo application tasks,\r
+ * which provide no particular functionality but do provide a good example of\r
+ * how to use the FreeRTOS API. In addition to the standard demo tasks, the \r
+ * following tasks and tests are defined and/or created within this file:\r
+ *\r
+ * "Reg test" tasks - These fill the registers with known values, then check\r
+ * that each register still contains its expected value. Each task uses\r
+ * different values. The tasks run with very low priority so get preempted very\r
+ * frequently. A register containing an unexpected value is indicative of an\r
+ * error in the context switching mechanism. Both standard and floating point\r
+ * registers are checked. The nature of the reg test tasks necessitates that\r
+ * they are written in assembly code. They are defined in regtest.src.\r
+ *\r
+ * "math" tasks - These are a set of 8 tasks that perform various double\r
+ * precision floating point calculations in order to check that the tasks \r
+ * floating point registers are being correctly saved and restored during\r
+ * context switches. The math tasks are defined in flop.c.\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 toggle an LED every 5 seconds - the toggle\r
+ * rate increasing to 500ms being a visual indication that at least one task has\r
+ * reported unexpected behaviour.\r
+ *\r
+ * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application\r
+ * tasks are executing as expected and no errors have been reported in any \r
+ * tasks. The toggle rate increasing to 200ms indicates that at least one task\r
+ * has reported unexpected behaviour.\r
+ * \r
+ * *NOTE 2* This file and flop.c both demonstrate the use of \r
+ * xPortUsesFloatingPoint() which informs the kernel that a task should maintain\r
+ * a floating point context.\r
+ *\r
+ * *NOTE 3* vApplicationSetupTimerInterrupt() is called by the kernel to let\r
+ * the application set up a timer to generate the tick interrupt. In this\r
+ * example a compare match timer is used for this purpose. \r
+ * vApplicationTickHook() is used to clear the timer interrupt and relies on\r
+ * configUSE_TICK_HOOK being set to 1 in FreeRTOSConfig.h.\r
+ *\r
+ * *NOTE 4* The traceTASK_SWITCHED_IN and traceTASK_SWITCHED_OUT trace hooks\r
+ * are used to save and restore the floating point context respectively for\r
+ * those tasks that require it (those for which xPortUsesFloatingPoint() has\r
+ * been called).\r
+ * \r
+ */\r
+\r
/* Kernel includes. */\r
#include "FreeRTOS.h"\r
#include "task.h"\r
by at least one task. */\r
#define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )\r
\r
+/*\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 execute if a call to pvPortMalloc() fails.\r
+ * pvPortMalloc() is called internally by the kernel whenever a task, queue or\r
+ * semaphore is created. It is also called by various parts of the demo\r
+ * application. \r
+ */\r
void vApplicationMallocFailedHook( void );\r
+\r
+/*\r
+ * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1\r
+ * in FreeRTOSConfig.h. It is a hook function that is called on each iteration\r
+ * of the idle task. It is essential that code added to this hook function\r
+ * never attempts to block in any way (for example, call xQueueReceive() with\r
+ * a block time specified). If the application makes use of the vTaskDelete()\r
+ * API function (as this demo application does) then it is also important that\r
+ * vApplicationIdleHook() is permitted to return to its calling function because\r
+ * it is the responsibility of the idle task to clean up memory allocated by the\r
+ * kernel to any task that has since been deleted.\r
+ */\r
void vApplicationIdleHook( void );\r
+\r
+/*\r
+ * Just sets up clocks, ports, etc. used by the demo application.\r
+ */\r
static void prvSetupHardware( void );\r
+\r
+/*\r
+ * The check task as described at the top of this file.\r
+ */\r
static void prvCheckTask( void *pvParameters );\r
\r
+/*\r
+ * The reg test tasks as described at the top of this file.\r
+ */\r
extern void vRegTest1Task( void *pvParameters );\r
extern void vRegTest2Task( void *pvParameters );\r
\r
+/*-----------------------------------------------------------*/\r
+\r
+/* Variables that are incremented on each iteration of the reg test tasks - \r
+provided the tasks have not reported any errors. The check task inspects these\r
+variables to ensure they are still incrementing as expected. */\r
volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
\r
/*-----------------------------------------------------------*/\r
\r
+/*\r
+ * Creates the majority of the demo application tasks before starting the\r
+ * scheduler.\r
+ */\r
void main(void)\r
{\r
xTaskHandle xCreatedTask;\r
prvSetupHardware();\r
\r
/* Start the reg test tasks which test the context switching mechanism. */\r
- xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, ( void * ) 0x12345678UL, tskIDLE_PRIORITY, &xCreatedTask );\r
+ xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );\r
xPortUsesFloatingPoint( xCreatedTask );\r
\r
- xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, ( void * ) 0x11223344UL, tskIDLE_PRIORITY, &xCreatedTask );\r
+ xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );\r
xPortUsesFloatingPoint( xCreatedTask );\r
\r
/* Start the check task as described at the top of this file. */\r
vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
vStartQueuePeekTasks();\r
vStartRecursiveMutexTasks();\r
+ \r
+ /* Start the math tasks as described at the top of this file. */\r
vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
\r
/* The suicide tasks must be created last as they need to know how many\r
/* Place this task in the blocked state until it is time to run again. */\r
vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
\r
- /* Inspect all the other tasks to esnure none have experienced any errors. */\r
+ /* Inspect all the other tasks to ensure none have experienced any errors. */\r
if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
{\r
/* Increase the rate at which this task cycles, which will increase the\r
\r
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
{\r
- /* Just to remove compiler warnings. */\r
+ /* Just to remove compiler warnings. This function will only actually\r
+ get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.\r
+ By default this demo does not use the stack overflow checking functionality\r
+ as the SuperH will normally execute an exception if the stack overflows. */\r
( void ) pxTask;\r
( void ) pcTaskName;\r
\r
void vApplicationSetupTimerInterrupt( void )\r
{\r
/* The peripheral clock is divided by 32 before feeding the compare match\r
-periphersl (CMT). */\r
+peripheral (CMT). */\r
unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;\r
\r
/* Configure a timer to create the RTOS tick interrupt. This example uses\r