]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/Common/Minimal/blocktim.c
Kernel source code:
[freertos] / FreeRTOS / Demo / Common / Minimal / blocktim.c
index b470595cd7ce2e2707109dca238c5b565e17969c..d53ceab74d9171609bfb589bf0f4b81b6160efe4 100644 (file)
 \r
 /* Task behaviour. */\r
 #define bktQUEUE_LENGTH                                ( 5 )\r
-#define bktSHORT_WAIT                          ( ( ( TickType_t ) 20 ) / portTICK_PERIOD_MS )\r
+#define bktSHORT_WAIT                          pdMS_TO_TICKS( ( TickType_t ) 20 )\r
 #define bktPRIMARY_BLOCK_TIME          ( 10 )\r
 #define bktALLOWABLE_MARGIN                    ( 15 )\r
 #define bktTIME_TO_BLOCK                       ( 175 )\r
 #define bktDONT_BLOCK                          ( ( TickType_t ) 0 )\r
 #define bktRUN_INDICATOR                       ( ( UBaseType_t ) 0x55 )\r
 \r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * The two test tasks.  Their behaviour is commented within the files.\r
+ */\r
+static void vPrimaryBlockTimeTestTask( void *pvParameters );\r
+static void vSecondaryBlockTimeTestTask( void *pvParameters );\r
+\r
+/*\r
+ * Very basic tests to verify the block times are as expected.\r
+ */\r
+static void prvBasicDelayTests( void );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
 /* The queue on which the tasks block. */\r
 static QueueHandle_t xTestQueue;\r
 \r
@@ -114,10 +129,6 @@ static volatile BaseType_t xErrorOccurred = pdFALSE;
 secondary task has executed. */\r
 static volatile UBaseType_t xRunIndicator;\r
 \r
-/* The two test tasks.  Their behaviour is commented within the files. */\r
-static void vPrimaryBlockTimeTestTask( void *pvParameters );\r
-static void vSecondaryBlockTimeTestTask( void *pvParameters );\r
-\r
 /*-----------------------------------------------------------*/\r
 \r
 void vCreateBlockTimeTasks( void )\r
@@ -150,9 +161,16 @@ TickType_t xTimeToBlock, xBlockedTime;
        for( ;; )\r
        {\r
                /*********************************************************************\r
-        Test 1\r
+               Test 0\r
+\r
+               Basic vTaskDelay() and vTaskDelayUntil() tests. */\r
+               prvBasicDelayTests();\r
+\r
+\r
+               /*********************************************************************\r
+               Test 1\r
 \r
-        Simple block time wakeup test on queue receives. */\r
+               Simple block time wakeup test on queue receives. */\r
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
                {\r
                        /* The queue is empty. Attempt to read from the queue using a block\r
@@ -187,9 +205,9 @@ TickType_t xTimeToBlock, xBlockedTime;
                }\r
 \r
                /*********************************************************************\r
-        Test 2\r
+               Test 2\r
 \r
-        Simple block time wakeup test on queue sends.\r
+               Simple block time wakeup test on queue sends.\r
 \r
                First fill the queue.  It should be empty so all sends should pass. */\r
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
@@ -238,7 +256,7 @@ TickType_t xTimeToBlock, xBlockedTime;
                }\r
 \r
                /*********************************************************************\r
-        Test 3\r
+               Test 3\r
 \r
                Wake the other task, it will block attempting to post to the queue.\r
                When we read from the queue the other task will wake, but before it\r
@@ -313,7 +331,7 @@ TickType_t xTimeToBlock, xBlockedTime;
 \r
 \r
                /*********************************************************************\r
-        Test 4\r
+               Test 4\r
 \r
                As per test 3 - but with the send and receive the other way around.\r
                The other task blocks attempting to read from the queue.\r
@@ -400,21 +418,21 @@ BaseType_t xData;
        for( ;; )\r
        {\r
                /*********************************************************************\r
-        Test 1 and 2\r
+               Test 0, 1 and 2\r
 \r
                This task does does not participate in these tests. */\r
                vTaskSuspend( NULL );\r
 \r
                /*********************************************************************\r
-        Test 3\r
+               Test 3\r
 \r
                The first thing we do is attempt to read from the queue.  It should be\r
                full so we block.  Note the time before we block so we can check the\r
                wake time is as per that expected. */\r
                xTimeWhenBlocking = xTaskGetTickCount();\r
 \r
-               /* We should unblock after bktTIME_TO_BLOCK having not sent\r
-               anything to the queue. */\r
+               /* We should unblock after bktTIME_TO_BLOCK having not sent anything to\r
+               the queue. */\r
                xData = 0;\r
                xRunIndicator = bktRUN_INDICATOR;\r
                if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )\r
@@ -480,6 +498,52 @@ BaseType_t xData;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+static void prvBasicDelayTests( void )\r
+{\r
+TickType_t xPreTime, xPostTime, x, xLastUnblockTime, xExpectedUnblockTime;\r
+const TickType_t xPeriod = 75, xCycles = 5, xAllowableMargin = ( bktALLOWABLE_MARGIN >> 1 );\r
+\r
+       /* Temporarily increase priority so the timing is more accurate, but not so\r
+       high as to disrupt the timer tests. */\r
+       vTaskPrioritySet( NULL, configTIMER_TASK_PRIORITY - 1 );\r
+\r
+       /* Crude check to too that vTaskDelay() blocks for the expected period. */\r
+       xPreTime = xTaskGetTickCount();\r
+       vTaskDelay( bktTIME_TO_BLOCK );\r
+       xPostTime = xTaskGetTickCount();\r
+\r
+       /* The priority is higher, so the allowable margin is halved when compared\r
+       to the other tests in this file. */\r
+       if( ( xPostTime - xPreTime ) > ( bktTIME_TO_BLOCK + xAllowableMargin ) )\r
+       {\r
+               xErrorOccurred = pdTRUE;\r
+       }\r
+\r
+       /* Now crude tests to check the vTaskDelayUntil() functionality. */\r
+       xPostTime = xTaskGetTickCount();\r
+       xLastUnblockTime = xPostTime;\r
+\r
+       for( x = 0; x < xCycles; x++ )\r
+       {\r
+               /* Calculate the next expected unblock time from the time taken before\r
+               this loop was entered. */\r
+               xExpectedUnblockTime = xPostTime + ( x * xPeriod );\r
+\r
+               vTaskDelayUntil( &xLastUnblockTime, xPeriod );\r
+\r
+               if( ( xTaskGetTickCount() - xExpectedUnblockTime ) > ( bktTIME_TO_BLOCK + xAllowableMargin ) )\r
+               {\r
+                       xErrorOccurred = pdTRUE;\r
+               }\r
+\r
+               xPrimaryCycles++;\r
+       }\r
+\r
+       /* Reset to the original task priority ready for the other tests. */\r
+       vTaskPrioritySet( NULL, bktPRIMARY_PRIORITY );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 BaseType_t xAreBlockTimeTestTasksStillRunning( void )\r
 {\r
 static BaseType_t xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;\r