2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
50 * to which is guarded by a semaphore.
\r
52 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
53 * semaphore a task checks to ensure that the guarded variable has an expected
\r
54 * value. It then clears the variable to zero before counting it back up to the
\r
55 * expected value in increments of 1. After each increment the variable is checked
\r
56 * to ensure it contains the value to which it was just set. When the starting
\r
57 * value is again reached the task releases the semaphore giving the other task in
\r
58 * the set a chance to do exactly the same thing. The starting value is high
\r
59 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
61 * An error is flagged if at any time during the process a shared variable is
\r
62 * found to have a value other than that expected. Such an occurrence would
\r
63 * suggest an error in the mutual exclusion mechanism by which access to the
\r
64 * variable is restricted.
\r
66 * The first set of two tasks poll their semaphore. The second set use blocking
\r
69 * \page SemTestC semtest.c
\r
70 * \ingroup DemoFiles
\r
75 Changes from V1.2.0:
\r
77 + The tasks that operate at the idle priority now use a lower expected
\r
78 count than those running at a higher priority. This prevents the low
\r
79 priority tasks from signaling an error because they have not been
\r
80 scheduled enough time for each of them to count the shared variable to
\r
85 + Delay periods are now specified using variables and constants of
\r
86 portTickType rather than unsigned portLONG.
\r
90 + The stack size now uses configMINIMAL_STACK_SIZE.
\r
91 + String constants made file scope to decrease stack depth on 8051 port.
\r
96 /* Scheduler include files. */
\r
97 #include "FreeRTOS.h"
\r
101 /* Demo app include files. */
\r
102 #include "semtest.h"
\r
105 /* The value to which the shared variables are counted. */
\r
106 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xfff )
\r
107 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xff )
\r
109 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
111 #define semtstNUM_TASKS ( 4 )
\r
113 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
115 /* The task function as described at the top of the file. */
\r
116 static void prvSemaphoreTest( void *pvParameters );
\r
118 /* Structure used to pass parameters to each task. */
\r
119 typedef struct SEMAPHORE_PARAMETERS
\r
121 xSemaphoreHandle xSemaphore;
\r
122 volatile unsigned portLONG *pulSharedVariable;
\r
123 portTickType xBlockTime;
\r
124 } xSemaphoreParameters;
\r
126 /* Variables used to check that all the tasks are still running without errors. */
\r
127 static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
128 static volatile portSHORT sNextCheckVariable = 0;
\r
130 /* Strings to print if USE_STDIO is defined. */
\r
131 const portCHAR * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";
\r
132 const portCHAR * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";
\r
134 /*-----------------------------------------------------------*/
\r
136 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
138 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
139 const portTickType xBlockTime = ( portTickType ) 100;
\r
141 /* Create the structure used to pass parameters to the first two tasks. */
\r
142 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
144 if( pxFirstSemaphoreParameters != NULL )
\r
146 /* Create the semaphore used by the first two tasks. */
\r
147 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
149 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
151 /* Create the variable which is to be shared by the first two tasks. */
\r
152 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
154 /* Initialise the share variable to the value the tasks expect. */
\r
155 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
157 /* The first two tasks do not block on semaphore calls. */
\r
158 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
160 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
161 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
162 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
166 /* Do exactly the same to create the second set of tasks, only this time
\r
167 provide a block time for the semaphore calls. */
\r
168 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
169 if( pxSecondSemaphoreParameters != NULL )
\r
171 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
173 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
175 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
176 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
177 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
179 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
180 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
184 /*-----------------------------------------------------------*/
\r
186 static void prvSemaphoreTest( void *pvParameters )
\r
188 xSemaphoreParameters *pxParameters;
\r
189 volatile unsigned portLONG *pulSharedVariable, ulExpectedValue;
\r
190 unsigned portLONG ulCounter;
\r
191 portSHORT sError = pdFALSE, sCheckVariableToUse;
\r
193 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
195 portENTER_CRITICAL();
\r
196 sCheckVariableToUse = sNextCheckVariable;
\r
197 sNextCheckVariable++;
\r
198 portEXIT_CRITICAL();
\r
200 /* Queue a message for printing to say the task has started. */
\r
201 vPrintDisplayMessage( &pcSemaphoreTaskStart );
\r
203 /* A structure is passed in as the parameter. This contains the shared
\r
204 variable being guarded. */
\r
205 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
206 pulSharedVariable = pxParameters->pulSharedVariable;
\r
208 /* If we are blocking we use a much higher count to ensure loads of context
\r
209 switches occur during the count. */
\r
210 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
212 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
216 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
221 /* Try to obtain the semaphore. */
\r
222 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
224 /* We have the semaphore and so expect any other tasks using the
\r
225 shared variable to have left it in the state we expect to find
\r
227 if( *pulSharedVariable != ulExpectedValue )
\r
229 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
233 /* Clear the variable, then count it back up to the expected value
\r
234 before releasing the semaphore. Would expect a context switch or
\r
235 two during this time. */
\r
236 for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
238 *pulSharedVariable = ulCounter;
\r
239 if( *pulSharedVariable != ulCounter )
\r
241 if( sError == pdFALSE )
\r
243 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
249 /* Release the semaphore, and if no errors have occurred increment the check
\r
251 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
253 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
257 if( sError == pdFALSE )
\r
259 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
261 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
265 /* If we have a block time then we are running at a priority higher
\r
266 than the idle priority. This task takes a long time to complete
\r
267 a cycle (deliberately so to test the guarding) so will be starving
\r
268 out lower priority tasks. Block for some time to allow give lower
\r
269 priority tasks some processor time. */
\r
270 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
274 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
276 /* We have not got the semaphore yet, so no point using the
\r
277 processor. We are not blocking when attempting to obtain the
\r
284 /*-----------------------------------------------------------*/
\r
286 /* This is called to check that all the created tasks are still running. */
\r
287 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
289 static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
290 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
292 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
294 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
299 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r