2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
54 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
55 * to which is guarded by a semaphore.
\r
57 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
58 * semaphore a task checks to ensure that the guarded variable has an expected
\r
59 * value. It then clears the variable to zero before counting it back up to the
\r
60 * expected value in increments of 1. After each increment the variable is checked
\r
61 * to ensure it contains the value to which it was just set. When the starting
\r
62 * value is again reached the task releases the semaphore giving the other task in
\r
63 * the set a chance to do exactly the same thing. The starting value is high
\r
64 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
66 * An error is flagged if at any time during the process a shared variable is
\r
67 * found to have a value other than that expected. Such an occurrence would
\r
68 * suggest an error in the mutual exclusion mechanism by which access to the
\r
69 * variable is restricted.
\r
71 * The first set of two tasks poll their semaphore. The second set use blocking
\r
74 * \page SemTestC semtest.c
\r
75 * \ingroup DemoFiles
\r
80 Changes from V1.2.0:
\r
82 + The tasks that operate at the idle priority now use a lower expected
\r
83 count than those running at a higher priority. This prevents the low
\r
84 priority tasks from signaling an error because they have not been
\r
85 scheduled enough time for each of them to count the shared variable to
\r
90 + Delay periods are now specified using variables and constants of
\r
91 portTickType rather than unsigned portLONG.
\r
95 + The stack size now uses configMINIMAL_STACK_SIZE.
\r
96 + String constants made file scope to decrease stack depth on 8051 port.
\r
101 /* Scheduler include files. */
\r
102 #include "FreeRTOS.h"
\r
104 #include "semphr.h"
\r
106 /* Demo app include files. */
\r
107 #include "semtest.h"
\r
110 /* The value to which the shared variables are counted. */
\r
111 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xfff )
\r
112 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xff )
\r
114 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
116 #define semtstNUM_TASKS ( 4 )
\r
118 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
120 /* The task function as described at the top of the file. */
\r
121 static void prvSemaphoreTest( void *pvParameters );
\r
123 /* Structure used to pass parameters to each task. */
\r
124 typedef struct SEMAPHORE_PARAMETERS
\r
126 xSemaphoreHandle xSemaphore;
\r
127 volatile unsigned portLONG *pulSharedVariable;
\r
128 portTickType xBlockTime;
\r
129 } xSemaphoreParameters;
\r
131 /* Variables used to check that all the tasks are still running without errors. */
\r
132 static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
133 static volatile portSHORT sNextCheckVariable = 0;
\r
135 /* Strings to print if USE_STDIO is defined. */
\r
136 const portCHAR * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";
\r
137 const portCHAR * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";
\r
139 /*-----------------------------------------------------------*/
\r
141 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
143 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
144 const portTickType xBlockTime = ( portTickType ) 100;
\r
146 /* Create the structure used to pass parameters to the first two tasks. */
\r
147 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
149 if( pxFirstSemaphoreParameters != NULL )
\r
151 /* Create the semaphore used by the first two tasks. */
\r
152 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
154 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
156 /* Create the variable which is to be shared by the first two tasks. */
\r
157 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
159 /* Initialise the share variable to the value the tasks expect. */
\r
160 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
162 /* The first two tasks do not block on semaphore calls. */
\r
163 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
165 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
166 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
167 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
171 /* Do exactly the same to create the second set of tasks, only this time
\r
172 provide a block time for the semaphore calls. */
\r
173 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
174 if( pxSecondSemaphoreParameters != NULL )
\r
176 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
178 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
180 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
181 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
182 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
184 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
185 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
189 /*-----------------------------------------------------------*/
\r
191 static void prvSemaphoreTest( void *pvParameters )
\r
193 xSemaphoreParameters *pxParameters;
\r
194 volatile unsigned portLONG *pulSharedVariable, ulExpectedValue;
\r
195 unsigned portLONG ulCounter;
\r
196 portSHORT sError = pdFALSE, sCheckVariableToUse;
\r
198 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
200 portENTER_CRITICAL();
\r
201 sCheckVariableToUse = sNextCheckVariable;
\r
202 sNextCheckVariable++;
\r
203 portEXIT_CRITICAL();
\r
205 /* Queue a message for printing to say the task has started. */
\r
206 vPrintDisplayMessage( &pcSemaphoreTaskStart );
\r
208 /* A structure is passed in as the parameter. This contains the shared
\r
209 variable being guarded. */
\r
210 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
211 pulSharedVariable = pxParameters->pulSharedVariable;
\r
213 /* If we are blocking we use a much higher count to ensure loads of context
\r
214 switches occur during the count. */
\r
215 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
217 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
221 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
226 /* Try to obtain the semaphore. */
\r
227 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
229 /* We have the semaphore and so expect any other tasks using the
\r
230 shared variable to have left it in the state we expect to find
\r
232 if( *pulSharedVariable != ulExpectedValue )
\r
234 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
238 /* Clear the variable, then count it back up to the expected value
\r
239 before releasing the semaphore. Would expect a context switch or
\r
240 two during this time. */
\r
241 for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
243 *pulSharedVariable = ulCounter;
\r
244 if( *pulSharedVariable != ulCounter )
\r
246 if( sError == pdFALSE )
\r
248 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
254 /* Release the semaphore, and if no errors have occurred increment the check
\r
256 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
258 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
262 if( sError == pdFALSE )
\r
264 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
266 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
270 /* If we have a block time then we are running at a priority higher
\r
271 than the idle priority. This task takes a long time to complete
\r
272 a cycle (deliberately so to test the guarding) so will be starving
\r
273 out lower priority tasks. Block for some time to allow give lower
\r
274 priority tasks some processor time. */
\r
275 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
279 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
281 /* We have not got the semaphore yet, so no point using the
\r
282 processor. We are not blocking when attempting to obtain the
\r
289 /*-----------------------------------------------------------*/
\r
291 /* This is called to check that all the created tasks are still running. */
\r
292 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
294 static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
295 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
297 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
299 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
304 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r