2 FreeRTOS.org V5.3.0 - 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the 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.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
54 * to which is guarded by a semaphore.
\r
56 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
57 * semaphore a task checks to ensure that the guarded variable has an expected
\r
58 * value. It then clears the variable to zero before counting it back up to the
\r
59 * expected value in increments of 1. After each increment the variable is checked
\r
60 * to ensure it contains the value to which it was just set. When the starting
\r
61 * value is again reached the task releases the semaphore giving the other task in
\r
62 * the set a chance to do exactly the same thing. The starting value is high
\r
63 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
65 * An error is flagged if at any time during the process a shared variable is
\r
66 * found to have a value other than that expected. Such an occurrence would
\r
67 * suggest an error in the mutual exclusion mechanism by which access to the
\r
68 * variable is restricted.
\r
70 * The first set of two tasks poll their semaphore. The second set use blocking
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Demo app include files. */
\r
84 #include "semtest.h"
\r
86 /* The value to which the shared variables are counted. */
\r
87 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xfff )
\r
88 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xff )
\r
90 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
92 #define semtstNUM_TASKS ( 4 )
\r
94 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
96 /* The task function as described at the top of the file. */
\r
97 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
\r
99 /* Structure used to pass parameters to each task. */
\r
100 typedef struct SEMAPHORE_PARAMETERS
\r
102 xSemaphoreHandle xSemaphore;
\r
103 volatile unsigned portLONG *pulSharedVariable;
\r
104 portTickType xBlockTime;
\r
105 } xSemaphoreParameters;
\r
107 /* Variables used to check that all the tasks are still running without errors. */
\r
108 static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
109 static volatile portSHORT sNextCheckVariable = 0;
\r
111 /*-----------------------------------------------------------*/
\r
113 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
115 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
116 const portTickType xBlockTime = ( portTickType ) 100;
\r
118 /* Create the structure used to pass parameters to the first two tasks. */
\r
119 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
121 if( pxFirstSemaphoreParameters != NULL )
\r
123 /* Create the semaphore used by the first two tasks. */
\r
124 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
126 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
128 /* Create the variable which is to be shared by the first two tasks. */
\r
129 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
131 /* Initialise the share variable to the value the tasks expect. */
\r
132 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
134 /* The first two tasks do not block on semaphore calls. */
\r
135 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
137 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
138 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
139 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
143 /* Do exactly the same to create the second set of tasks, only this time
\r
144 provide a block time for the semaphore calls. */
\r
145 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
146 if( pxSecondSemaphoreParameters != NULL )
\r
148 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
150 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
152 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
153 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
154 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
156 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
157 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
161 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
162 in use. The registry is provided as a means for kernel aware
\r
163 debuggers to locate semaphores and has no purpose if a kernel aware debugger
\r
164 is not being used. The call to vQueueAddToRegistry() will be removed
\r
165 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
166 defined to be less than 1. */
\r
167 vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );
\r
168 vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );
\r
170 /*-----------------------------------------------------------*/
\r
172 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
\r
174 xSemaphoreParameters *pxParameters;
\r
175 volatile unsigned portLONG *pulSharedVariable, ulExpectedValue;
\r
176 unsigned portLONG ulCounter;
\r
177 portSHORT sError = pdFALSE, sCheckVariableToUse;
\r
179 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
181 portENTER_CRITICAL();
\r
182 sCheckVariableToUse = sNextCheckVariable;
\r
183 sNextCheckVariable++;
\r
184 portEXIT_CRITICAL();
\r
186 /* A structure is passed in as the parameter. This contains the shared
\r
187 variable being guarded. */
\r
188 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
189 pulSharedVariable = pxParameters->pulSharedVariable;
\r
191 /* If we are blocking we use a much higher count to ensure loads of context
\r
192 switches occur during the count. */
\r
193 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
195 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
199 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
204 /* Try to obtain the semaphore. */
\r
205 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
207 /* We have the semaphore and so expect any other tasks using the
\r
208 shared variable to have left it in the state we expect to find
\r
210 if( *pulSharedVariable != ulExpectedValue )
\r
215 /* Clear the variable, then count it back up to the expected value
\r
216 before releasing the semaphore. Would expect a context switch or
\r
217 two during this time. */
\r
218 for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
220 *pulSharedVariable = ulCounter;
\r
221 if( *pulSharedVariable != ulCounter )
\r
227 /* Release the semaphore, and if no errors have occurred increment the check
\r
229 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
234 if( sError == pdFALSE )
\r
236 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
238 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
242 /* If we have a block time then we are running at a priority higher
\r
243 than the idle priority. This task takes a long time to complete
\r
244 a cycle (deliberately so to test the guarding) so will be starving
\r
245 out lower priority tasks. Block for some time to allow give lower
\r
246 priority tasks some processor time. */
\r
247 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
251 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
253 /* We have not got the semaphore yet, so no point using the
\r
254 processor. We are not blocking when attempting to obtain the
\r
261 /*-----------------------------------------------------------*/
\r
263 /* This is called to check that all the created tasks are still running. */
\r
264 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
266 static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
267 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
269 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
271 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
276 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r