]> git.sur5r.net Git - freertos/commitdiff
Add additional configASSERTS() to some standard demo tasks.
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Sun, 1 Sep 2013 19:45:06 +0000 (19:45 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Sun, 1 Sep 2013 19:45:06 +0000 (19:45 +0000)
Use own rand() function in QueueSet.c to prevent library versions being referenced.

git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2014 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

FreeRTOS/Demo/Common/Minimal/QueueSet.c
FreeRTOS/Demo/Common/Minimal/dynamic.c
FreeRTOS/Demo/Common/Minimal/recmutex.c

index 5ffc45894b9e60b9bc6991d0758bcd1d4c214200..01dcb2a6c4a88e939040ed197662a92abe98a503 100644 (file)
@@ -109,7 +109,7 @@ in the range of 0xffff to ULONG_MAX. */
 \r
 /* For test purposes the priority of the sending task is changed after every\r
 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */\r
-#define queuesetPRIORITY_CHANGE_LOOPS  100UL\r
+#define queuesetPRIORITY_CHANGE_LOOPS  ( ( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH ) * 3 )\r
 \r
 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */\r
 #define queuesetISR_TX_PERIOD  ( 100UL )\r
@@ -161,6 +161,13 @@ static void prvSetupTest( xTaskHandle xQueueSetSendingTask );
  */\r
 static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived );\r
 \r
+/*\r
+ * Local pseudo random number seed and return functions.  Used to avoid calls\r
+ * to the standard library.\r
+ */\r
+static unsigned long prvRand( void );\r
+static void prvSRand( unsigned long ulSeed );\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /* The queues that are added to the set. */\r
@@ -193,6 +200,9 @@ xAreQueeuSetTasksStillRunning() function can check it is incrementing as
 expected. */\r
 static volatile unsigned long ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
 \r
+/* Used by the pseudo random number generator. */\r
+static unsigned long ulNextRand = 0;\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 void vStartQueueSetTasks( void )\r
@@ -264,25 +274,25 @@ portBASE_TYPE xReturn = pdPASS, x;
 \r
 static void prvQueueSetSendingTask( void *pvParameters )\r
 {\r
-unsigned long ulTaskTxValue = 0;\r
-portBASE_TYPE xQueueToWriteTo;\r
+unsigned long ulTaskTxValue = 0, ulQueueToWriteTo;\r
 xQueueHandle xQueueInUse;\r
 unsigned portBASE_TYPE uxPriority = queuesetMEDIUM_PRIORITY, ulLoops = 0;\r
 \r
        /* Remove compiler warning about the unused parameter. */\r
        ( void ) pvParameters;\r
 \r
-       srand( ( unsigned int ) &ulTaskTxValue );\r
+       /* Seed mini pseudo random number generator. */\r
+       prvSRand( ( unsigned long ) &ulTaskTxValue );\r
 \r
        for( ;; )\r
        {\r
                /* Generate the index for the queue to which a value is to be sent. */\r
-               xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;\r
-               xQueueInUse = xQueues[ xQueueToWriteTo ];\r
+               ulQueueToWriteTo = prvRand() % queuesetNUM_QUEUES_IN_SET;\r
+               xQueueInUse = xQueues[ ulQueueToWriteTo ];\r
 \r
                /* Note which index is being written to to ensure all the queues are\r
                used. */\r
-               ( ulQueueUsedCounter[ xQueueToWriteTo ] )++;\r
+               ( ulQueueUsedCounter[ ulQueueToWriteTo ] )++;\r
 \r
                /* Send to the queue to unblock the task that is waiting for data to\r
                arrive on a queue within the queue set to which this queue belongs. */\r
@@ -637,3 +647,17 @@ unsigned long ulValueToSend = 0;
        /* Let the ISR access the queues also. */\r
        xSetupComplete = pdTRUE;\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+static unsigned long prvRand( void )\r
+{\r
+       ulNextRand = ( ulNextRand * 1103515245UL ) + 12345UL;\r
+    return (ulNextRand / 65536UL ) % 32768UL;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSRand( unsigned long ulSeed )\r
+{\r
+    ulNextRand = ulSeed;\r
+}\r
+\r
index fe3c0a67783bbaa981fcdf00a07e1c25660677d7..13d9b2fd070ebc665b745979fa7c2c0ef82a475f 100644 (file)
  *\r
  * One counter task loops indefinitely, incrementing the shared count variable\r
  * on each iteration.  To ensure it has exclusive access to the variable it\r
- * raises it's priority above that of the controller task before each\r
- * increment, lowering it again to it's original priority before starting the\r
+ * raises its priority above that of the controller task before each\r
+ * increment, lowering it again to its original priority before starting the\r
  * next iteration.\r
  *\r
  * The other counter task increments the shared count variable on each\r
- * iteration of it's loop until the count has reached a limit of 0xff - at\r
+ * iteration of its loop until the count has reached a limit of 0xff - at\r
  * which point it suspends itself.  It will not start a new loop until the\r
- * controller task has made it "ready" again by calling vTaskResume ().\r
+ * controller task has made it "ready" again by calling vTaskResume().\r
  * This second counter task operates at a higher priority than controller\r
  * task so does not need to worry about mutual exclusion of the counter\r
  * variable.\r
  * continuous count task, and moves on to its second section.\r
  *\r
  * At the start of the second section the shared variable is cleared to zero.\r
- * The limited count task is then woken from it's suspension by a call to\r
+ * The limited count task is then woken from its suspension by a call to\r
  * vTaskResume ().  As this counter task operates at a higher priority than\r
  * the controller task the controller task should not run again until the\r
  * shared variable has been counted up to the limited value causing the counter\r
@@ -153,11 +153,11 @@ static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
 \r
 /* Handles to the two counter tasks.  These could be passed in as parameters\r
 to the controller task to prevent them having to be file scope. */\r
-static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;\r
+static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle;\r
 \r
 /* The shared counter variable.  This is passed in as a parameter to the two\r
 counter variables for demonstration purposes. */\r
-static unsigned long ulCounter;\r
+static volatile unsigned long ulCounter;\r
 \r
 /* Variables used to check that the tasks are still operating without error.\r
 Each complete iteration of the controller task increments this variable\r
@@ -192,7 +192,7 @@ void vStartDynamicPriorityTasks( void )
        defined to be less than 1. */\r
        vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );\r
 \r
-       xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );\r
+       xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );\r
        xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
        xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
        xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
@@ -235,7 +235,7 @@ unsigned long *pulCounter;
  */\r
 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
 {\r
-unsigned long *pulCounter;\r
+volatile unsigned long *pulCounter;\r
 unsigned portBASE_TYPE uxOurPriority;\r
 \r
        /* Take a pointer to the shared variable from the parameters passed into\r
@@ -248,11 +248,15 @@ unsigned portBASE_TYPE uxOurPriority;
 \r
        for( ;; )\r
        {\r
-               /* Raise our priority above the controller task to ensure a context\r
-               switch does not occur while we are accessing this variable. */\r
+               /* Raise the priority above the controller task to ensure a context\r
+               switch does not occur while the variable is being accessed. */\r
                vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
+               {\r
+                       configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );\r
                        ( *pulCounter )++;\r
+               }\r
                vTaskPrioritySet( NULL, uxOurPriority );\r
+               configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -280,10 +284,26 @@ short sError = pdFALSE;
                for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
                {\r
                        /* Suspend the continuous count task so we can take a mirror of the\r
-                       shared variable without risk of corruption. */\r
-                       vTaskSuspend( xContinousIncrementHandle );\r
+                       shared variable without risk of corruption.  This is not really\r
+                       needed as the other task raises its priority above this task's\r
+                       priority. */\r
+                       vTaskSuspend( xContinuousIncrementHandle );\r
+                       {\r
+                               #if( INCLUDE_eTaskGetState == 1 )\r
+                               {\r
+                                       configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );\r
+                               }\r
+                               #endif /* INCLUDE_eTaskGetState */\r
+\r
                                ulLastCounter = ulCounter;\r
-                       vTaskResume( xContinousIncrementHandle );\r
+                       }\r
+                       vTaskResume( xContinuousIncrementHandle );\r
+\r
+                       #if( INCLUDE_eTaskGetState == 1 )\r
+                       {\r
+                               configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );\r
+                       }\r
+                       #endif /* INCLUDE_eTaskGetState */\r
 \r
                        /* Now delay to ensure the other task has processor time. */\r
                        vTaskDelay( priSLEEP_TIME );\r
@@ -303,20 +323,34 @@ short sError = pdFALSE;
                        xTaskResumeAll();\r
                }\r
 \r
-\r
                /* Second section: */\r
 \r
-               /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
-               vTaskSuspend( xContinousIncrementHandle );\r
+               /* Suspend the continuous counter task so it stops accessing the shared\r
+               variable. */\r
+               vTaskSuspend( xContinuousIncrementHandle );\r
 \r
                /* Reset the variable. */\r
                ulCounter = ( unsigned long ) 0;\r
 \r
+               #if( INCLUDE_eTaskGetState == 1 )\r
+               {\r
+                       configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
+               }\r
+               #endif /* INCLUDE_eTaskGetState */\r
+\r
                /* Resume the limited count task which has a higher priority than us.\r
                We should therefore not return from this call until the limited count\r
                task has suspended itself with a known value in the counter variable. */\r
                vTaskResume( xLimitedIncrementHandle );\r
 \r
+               /* This task should not run again until xLimitedIncrementHandle has\r
+               suspended itself. */\r
+               #if( INCLUDE_eTaskGetState == 1 )\r
+               {\r
+                       configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
+               }\r
+               #endif /* INCLUDE_eTaskGetState */\r
+\r
                /* Does the counter variable have the expected value? */\r
                if( ulCounter != priMAX_COUNT )\r
                {\r
@@ -332,7 +366,7 @@ short sError = pdFALSE;
                }\r
 \r
                /* Resume the continuous count task and do it all again. */\r
-               vTaskResume( xContinousIncrementHandle );\r
+               vTaskResume( xContinuousIncrementHandle );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
index 79a0b09f33415cd85dd835fec4672f90d79bdcfa..bce86e10711baddd98310f0a634e9b7d5a89ad20 100644 (file)
 \r
        This demo creates three tasks all of which access the same recursive mutex:\r
 \r
-       prvRecursiveMutexControllingTask() has the highest priority so executes \r
-       first and grabs the mutex.  It then performs some recursive accesses - \r
-       between each of which it sleeps for a short period to let the lower \r
+       prvRecursiveMutexControllingTask() has the highest priority so executes\r
+       first and grabs the mutex.  It then performs some recursive accesses -\r
+       between each of which it sleeps for a short period to let the lower\r
        priority tasks execute.  When it has completed its demo functionality\r
        it gives the mutex back before suspending itself.\r
 \r
        prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
-       a blocking 'take'.  The blocking task has a lower priority than the \r
+       a blocking 'take'.  The blocking task has a lower priority than the\r
        controlling     task so by the time it executes the mutex has already been\r
-       taken by the controlling task,  causing the blocking task to block.  It \r
-       does not unblock until the controlling task has given the mutex back, \r
-       and it does not actually run until the controlling task has suspended \r
+       taken by the controlling task,  causing the blocking task to block.  It\r
+       does not unblock until the controlling task has given the mutex back,\r
+       and it does not actually run until the controlling task has suspended\r
        itself (due to the relative priorities).  When it eventually does obtain\r
-       the mutex all it does is give the mutex back prior to also suspending \r
-       itself.  At this point both the controlling task and the blocking task are \r
+       the mutex all it does is give the mutex back prior to also suspending\r
+       itself.  At this point both the controlling task and the blocking task are\r
        suspended.\r
 \r
        prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round\r
        a tight loop attempting to obtain the mutex with a non-blocking call.  As\r
        the lowest priority task it will not successfully obtain the mutex until\r
-       both the controlling and blocking tasks are suspended.  Once it eventually \r
+       both the controlling and blocking tasks are suspended.  Once it eventually\r
        does obtain the mutex it first unsuspends both the controlling task and\r
        blocking task prior to giving the mutex back - resulting in the polling\r
        task temporarily inheriting the controlling tasks priority.\r
 /* Misc. */\r
 #define recmuSHORT_DELAY                               ( 20 / portTICK_RATE_MS )\r
 #define recmuNO_DELAY                                  ( ( portTickType ) 0 )\r
-#define recmuTWO_TICK_DELAY                            ( ( portTickType ) 2 )\r
+#define recmuTHREE_TICK_DELAY                  ( ( portTickType ) 3 )\r
 \r
 /* The three tasks as described at the top of this file. */\r
 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
@@ -131,7 +131,7 @@ static xSemaphoreHandle xMutex;
 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
 \r
-/* Handles of the two higher priority tasks, required so they can be resumed \r
+/* Handles of the two higher priority tasks, required so they can be resumed\r
 (unsuspended). */\r
 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;\r
 \r
@@ -144,10 +144,10 @@ void vStartRecursiveMutexTasks( void )
        xMutex = xSemaphoreCreateRecursiveMutex();\r
 \r
        /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
-       in use.  The registry is provided as a means for kernel aware \r
+       in use.  The registry is provided as a means for kernel aware\r
        debuggers to locate mutex and has no purpose if a kernel aware debugger\r
        is not being used.  The call to vQueueAddToRegistry() will be removed\r
-       by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
+       by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
        defined to be less than 1. */\r
        vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );\r
 \r
@@ -183,7 +183,7 @@ unsigned portBASE_TYPE ux;
                {\r
                        /* We should now be able to take the mutex as many times as\r
                        we like.\r
-                       \r
+\r
                        The first time through the mutex will be immediately available, on\r
                        subsequent times through the mutex will be held by the polling task\r
                        at this point and this Take will cause the polling task to inherit\r
@@ -191,14 +191,14 @@ unsigned portBASE_TYPE ux;
                        long enough to ensure the polling task will execute again before the\r
                        block time expires.  If the block time does expire then the error\r
                        flag will be set here. */\r
-                       if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )\r
+                       if( xSemaphoreTakeRecursive( xMutex, recmuTHREE_TICK_DELAY ) != pdPASS )\r
                        {\r
                                xErrorOccurred = pdTRUE;\r
                        }\r
 \r
                        /* Ensure the other task attempting to access the mutex (and the\r
                        other demo tasks) are able to execute to ensure they either block\r
-                       (where a block time is specified) or return an error (where no \r
+                       (where a block time is specified) or return an error (where no\r
                        block time is specified) as the mutex is held by this task. */\r
                        vTaskDelay( recmuSHORT_DELAY );\r
                }\r
@@ -228,7 +228,7 @@ unsigned portBASE_TYPE ux;
                        xErrorOccurred = pdTRUE;\r
                }\r
 \r
-               /* Keep count of the number of cycles this task has performed so a \r
+               /* Keep count of the number of cycles this task has performed so a\r
                stall can be detected. */\r
                uxControllingCycles++;\r
 \r
@@ -249,10 +249,12 @@ static void prvRecursiveMutexBlockingTask( void *pvParameters )
        {\r
                /* This task will run while the controlling task is blocked, and the\r
                controlling task will block only once it has the mutex - therefore\r
-               this call should block until the controlling task has given up the \r
-               mutex, and not actually execute past this call until the controlling \r
-               task is suspended. */\r
-               if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )\r
+               this call should block until the controlling task has given up the\r
+               mutex, and not actually execute past this call until the controlling\r
+               task is suspended.  portMAX_DELAY - 1 is used instead of portMAX_DELAY\r
+               to ensure the task's state is reported as Blocked and not Suspended in\r
+               a later call to configASSERT() (within the polling task). */\r
+               if( xSemaphoreTakeRecursive( xMutex, ( portMAX_DELAY - 1 ) ) == pdPASS )\r
                {\r
                        if( xControllingIsSuspended != pdTRUE )\r
                        {\r
@@ -287,7 +289,7 @@ static void prvRecursiveMutexBlockingTask( void *pvParameters )
                        xErrorOccurred = pdTRUE;\r
                }\r
 \r
-               /* Keep count of the number of cycles this task has performed so a \r
+               /* Keep count of the number of cycles this task has performed so a\r
                stall can be detected. */\r
                uxBlockingCycles++;\r
        }\r
@@ -306,6 +308,13 @@ static void prvRecursiveMutexPollingTask( void *pvParameters )
                happen when the controlling task is also suspended. */\r
                if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
                {\r
+                       #if( INCLUDE_eTaskGetState == 1 )\r
+                       {\r
+                               configASSERT( eTaskGetState( xControllingTaskHandle ) == eSuspended );\r
+                               configASSERT( eTaskGetState( xBlockingTaskHandle ) == eSuspended );\r
+                       }\r
+                       #endif /* INCLUDE_eTaskGetState */\r
+\r
                        /* Is the blocking task suspended? */\r
                        if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
                        {\r
@@ -313,7 +322,7 @@ static void prvRecursiveMutexPollingTask( void *pvParameters )
                        }\r
                        else\r
                        {\r
-                               /* Keep count of the number of cycles this task has performed \r
+                               /* Keep count of the number of cycles this task has performed\r
                                so a stall can be detected. */\r
                                uxPollingCycles++;\r
 \r
@@ -328,14 +337,27 @@ static void prvRecursiveMutexPollingTask( void *pvParameters )
                                mutex by the time this fixed period has expired. */\r
                                vTaskResume( xBlockingTaskHandle );\r
                 vTaskResume( xControllingTaskHandle );\r
-                       \r
+\r
                                /* The other two tasks should now have executed and no longer\r
                                be suspended. */\r
                                if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
                                {\r
                                        xErrorOccurred = pdTRUE;\r
-                               }                               \r
-                       \r
+                               }\r
+\r
+                               #if( INCLUDE_uxTaskPriorityGet == 1 )\r
+                               {\r
+                                       configASSERT( uxTaskPriorityGet( NULL ) == recmuCONTROLLING_TASK_PRIORITY );\r
+                               }\r
+                               #endif /* INCLUDE_uxTaskPriorityGet */\r
+\r
+                               #if( INCLUDE_eTaskGetState == 1 )\r
+                               {\r
+                                       configASSERT( eTaskGetState( xControllingTaskHandle ) == eBlocked );\r
+                                       configASSERT( eTaskGetState( xBlockingTaskHandle ) == eBlocked );\r
+                               }\r
+                               #endif /* INCLUDE_eTaskGetState */\r
+\r
                                /* Release the mutex, disinheriting the higher priority again. */\r
                                if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
                                {\r