FreeRTOS Real Time Operating System, Modified from Kenji Arai's initial port. See freertos.org for full documentation.

Fork of FreeRTOS_on_mbed_v1 by Kenji Arai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers semtest.c Source File

semtest.c

00001 /*
00002     FreeRTOS V6.0.3 - Copyright (C) 2010 Real Time Engineers Ltd.
00003 
00004     ***************************************************************************
00005     *                                                                         *
00006     * If you are:                                                             *
00007     *                                                                         *
00008     *    + New to FreeRTOS,                                                   *
00009     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
00010     *    + Looking for basic training,                                        *
00011     *    + Wanting to improve your FreeRTOS skills and productivity           *
00012     *                                                                         *
00013     * then take a look at the FreeRTOS eBook                                  *
00014     *                                                                         *
00015     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
00016     *                  http://www.FreeRTOS.org/Documentation                  *
00017     *                                                                         *
00018     * A pdf reference manual is also available.  Both are usually delivered   *
00019     * to your inbox within 20 minutes to two hours when purchased between 8am *
00020     * and 8pm GMT (although please allow up to 24 hours in case of            *
00021     * exceptional circumstances).  Thank you for your support!                *
00022     *                                                                         *
00023     ***************************************************************************
00024 
00025     This file is part of the FreeRTOS distribution.
00026 
00027     FreeRTOS is free software; you can redistribute it and/or modify it under
00028     the terms of the GNU General Public License (version 2) as published by the
00029     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
00030     ***NOTE*** The exception to the GPL is included to allow you to distribute
00031     a combined work that includes FreeRTOS without being obliged to provide the
00032     source code for proprietary components outside of the FreeRTOS kernel.
00033     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
00034     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00035     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00036     more details. You should have received a copy of the GNU General Public 
00037     License and the FreeRTOS license exception along with FreeRTOS; if not it 
00038     can be viewed here: http://www.freertos.org/a00114.html and also obtained 
00039     by writing to Richard Barry, contact details for whom are available on the
00040     FreeRTOS WEB site.
00041 
00042     1 tab == 4 spaces!
00043 
00044     http://www.FreeRTOS.org - Documentation, latest information, license and
00045     contact details.
00046 
00047     http://www.SafeRTOS.com - A version that is certified for use in safety
00048     critical systems.
00049 
00050     http://www.OpenRTOS.com - Commercial support, development, porting,
00051     licensing and training services.
00052 */
00053 
00054 /*
00055  * Creates two sets of two tasks.  The tasks within a set share a variable, access 
00056  * to which is guarded by a semaphore.
00057  * 
00058  * Each task starts by attempting to obtain the semaphore.  On obtaining a 
00059  * semaphore a task checks to ensure that the guarded variable has an expected 
00060  * value.  It then clears the variable to zero before counting it back up to the 
00061  * expected value in increments of 1.  After each increment the variable is checked 
00062  * to ensure it contains the value to which it was just set. When the starting 
00063  * value is again reached the task releases the semaphore giving the other task in 
00064  * the set a chance to do exactly the same thing.  The starting value is high 
00065  * enough to ensure that a tick is likely to occur during the incrementing loop.
00066  *
00067  * An error is flagged if at any time during the process a shared variable is 
00068  * found to have a value other than that expected.  Such an occurrence would 
00069  * suggest an error in the mutual exclusion mechanism by which access to the 
00070  * variable is restricted.
00071  *
00072  * The first set of two tasks poll their semaphore.  The second set use blocking 
00073  * calls.
00074  *
00075  */
00076 
00077 
00078 #include <stdlib.h>
00079 
00080 /* Scheduler include files. */
00081 #include "FreeRTOS.h"
00082 #include "task.h"
00083 #include "semphr.h"
00084 
00085 
00086 /* Demo app include files. */
00087 #include "semtest.h"
00088 
00089 /* The value to which the shared variables are counted. */
00090 #define semtstBLOCKING_EXPECTED_VALUE        ( ( unsigned long ) 0xfff )
00091 #define semtstNON_BLOCKING_EXPECTED_VALUE    ( ( unsigned long ) 0xff  )
00092 
00093 #define semtstSTACK_SIZE            configMINIMAL_STACK_SIZE
00094 
00095 #define semtstNUM_TASKS                ( 4 )
00096 
00097 #define semtstDELAY_FACTOR            ( ( portTickType ) 10 )
00098 
00099 /* The task function as described at the top of the file. */
00100 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
00101 
00102 /* Structure used to pass parameters to each task. */
00103 typedef struct SEMAPHORE_PARAMETERS
00104 {
00105     xSemaphoreHandle xSemaphore;
00106     volatile unsigned long *pulSharedVariable;
00107     portTickType xBlockTime;
00108 } xSemaphoreParameters;
00109 
00110 /* Variables used to check that all the tasks are still running without errors. */
00111 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
00112 static volatile short sNextCheckVariable = 0;
00113 
00114 /*-----------------------------------------------------------*/
00115 
00116 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
00117 {
00118 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
00119 const portTickType xBlockTime = ( portTickType ) 100;
00120 
00121     /* Create the structure used to pass parameters to the first two tasks. */
00122     pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
00123 
00124     if( pxFirstSemaphoreParameters != NULL )
00125     {
00126         /* Create the semaphore used by the first two tasks. */
00127         vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
00128 
00129         if( pxFirstSemaphoreParameters->xSemaphore != NULL )
00130         {
00131             /* Create the variable which is to be shared by the first two tasks. */
00132             pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
00133 
00134             /* Initialise the share variable to the value the tasks expect. */
00135             *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
00136 
00137             /* The first two tasks do not block on semaphore calls. */
00138             pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
00139 
00140             /* Spawn the first two tasks.  As they poll they operate at the idle priority. */
00141             xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
00142             xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
00143         }
00144     }
00145 
00146     /* Do exactly the same to create the second set of tasks, only this time 
00147     provide a block time for the semaphore calls. */
00148     pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
00149     if( pxSecondSemaphoreParameters != NULL )
00150     {
00151         vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
00152 
00153         if( pxSecondSemaphoreParameters->xSemaphore != NULL )
00154         {
00155             pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
00156             *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
00157             pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
00158 
00159             xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
00160             xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
00161         }
00162     }
00163 
00164     /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
00165     in use.  The registry is provided as a means for kernel aware 
00166     debuggers to locate semaphores and has no purpose if a kernel aware debugger
00167     is not being used.  The call to vQueueAddToRegistry() will be removed
00168     by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is 
00169     defined to be less than 1. */
00170     vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_1" );
00171     vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_2" );
00172 }
00173 /*-----------------------------------------------------------*/
00174 
00175 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
00176 {
00177 xSemaphoreParameters *pxParameters;
00178 volatile unsigned long *pulSharedVariable, ulExpectedValue;
00179 unsigned long ulCounter;
00180 short sError = pdFALSE, sCheckVariableToUse;
00181 
00182     /* See which check variable to use.  sNextCheckVariable is not semaphore 
00183     protected! */
00184     portENTER_CRITICAL();
00185         sCheckVariableToUse = sNextCheckVariable;
00186         sNextCheckVariable++;
00187     portEXIT_CRITICAL();
00188 
00189     /* A structure is passed in as the parameter.  This contains the shared 
00190     variable being guarded. */
00191     pxParameters = ( xSemaphoreParameters * ) pvParameters;
00192     pulSharedVariable = pxParameters->pulSharedVariable;
00193 
00194     /* If we are blocking we use a much higher count to ensure loads of context
00195     switches occur during the count. */
00196     if( pxParameters->xBlockTime > ( portTickType ) 0 )
00197     {
00198         ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
00199     }
00200     else
00201     {
00202         ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
00203     }
00204 
00205     for( ;; )
00206     {
00207         /* Try to obtain the semaphore. */
00208         if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
00209         {
00210             /* We have the semaphore and so expect any other tasks using the
00211             shared variable to have left it in the state we expect to find
00212             it. */
00213             if( *pulSharedVariable != ulExpectedValue )
00214             {
00215                 sError = pdTRUE;
00216             }
00217             
00218             /* Clear the variable, then count it back up to the expected value
00219             before releasing the semaphore.  Would expect a context switch or
00220             two during this time. */
00221             for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
00222             {
00223                 *pulSharedVariable = ulCounter;
00224                 if( *pulSharedVariable != ulCounter )
00225                 {
00226                     sError = pdTRUE;
00227                 }
00228             }
00229 
00230             /* Release the semaphore, and if no errors have occurred increment the check
00231             variable. */
00232             if(    xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
00233             {
00234                 sError = pdTRUE;
00235             }
00236 
00237             if( sError == pdFALSE )
00238             {
00239                 if( sCheckVariableToUse < semtstNUM_TASKS )
00240                 {
00241                     ( sCheckVariables[ sCheckVariableToUse ] )++;
00242                 }
00243             }
00244 
00245             /* If we have a block time then we are running at a priority higher
00246             than the idle priority.  This task takes a long time to complete
00247             a cycle    (deliberately so to test the guarding) so will be starving
00248             out lower priority tasks.  Block for some time to allow give lower
00249             priority tasks some processor time. */
00250             vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
00251         }
00252         else
00253         {
00254             if( pxParameters->xBlockTime == ( portTickType ) 0 )
00255             {
00256                 /* We have not got the semaphore yet, so no point using the
00257                 processor.  We are not blocking when attempting to obtain the
00258                 semaphore. */
00259                 taskYIELD();
00260             }
00261         }
00262     }
00263 }
00264 /*-----------------------------------------------------------*/
00265 
00266 /* This is called to check that all the created tasks are still running. */
00267 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
00268 {
00269 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
00270 portBASE_TYPE xTask, xReturn = pdTRUE;
00271 
00272     for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
00273     {
00274         if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
00275         {
00276             xReturn = pdFALSE;
00277         }
00278 
00279         sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
00280     }
00281 
00282     return xReturn;
00283 }
00284 
00285