2 FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
56 * to which is guarded by a semaphore.
\r
58 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
59 * semaphore a task checks to ensure that the guarded variable has an expected
\r
60 * value. It then clears the variable to zero before counting it back up to the
\r
61 * expected value in increments of 1. After each increment the variable is checked
\r
62 * to ensure it contains the value to which it was just set. When the starting
\r
63 * value is again reached the task releases the semaphore giving the other task in
\r
64 * the set a chance to do exactly the same thing. The starting value is high
\r
65 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
67 * An error is flagged if at any time during the process a shared variable is
\r
68 * found to have a value other than that expected. Such an occurrence would
\r
69 * suggest an error in the mutual exclusion mechanism by which access to the
\r
70 * variable is restricted.
\r
72 * The first set of two tasks poll their semaphore. The second set use blocking
\r
75 * \page SemTestC semtest.c
\r
76 * \ingroup DemoFiles
\r
81 Changes from V1.2.0:
\r
83 + The tasks that operate at the idle priority now use a lower expected
\r
84 count than those running at a higher priority. This prevents the low
\r
85 priority tasks from signaling an error because they have not been
\r
86 scheduled enough time for each of them to count the shared variable to
\r
91 + Delay periods are now specified using variables and constants of
\r
92 portTickType rather than unsigned long.
\r
96 + The stack size now uses configMINIMAL_STACK_SIZE.
\r
97 + String constants made file scope to decrease stack depth on 8051 port.
\r
100 #include <stdlib.h>
\r
102 /* Scheduler include files. */
\r
103 #include "FreeRTOS.h"
\r
105 #include "semphr.h"
\r
107 /* Demo app include files. */
\r
108 #include "semtest.h"
\r
111 /* The value to which the shared variables are counted. */
\r
112 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xfff )
\r
113 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xff )
\r
115 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
117 #define semtstNUM_TASKS ( 4 )
\r
119 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
121 /* The task function as described at the top of the file. */
\r
122 static void prvSemaphoreTest( void *pvParameters );
\r
124 /* Structure used to pass parameters to each task. */
\r
125 typedef struct SEMAPHORE_PARAMETERS
\r
127 xSemaphoreHandle xSemaphore;
\r
128 volatile unsigned long *pulSharedVariable;
\r
129 portTickType xBlockTime;
\r
130 } xSemaphoreParameters;
\r
132 /* Variables used to check that all the tasks are still running without errors. */
\r
133 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
134 static volatile short sNextCheckVariable = 0;
\r
136 /* Strings to print if USE_STDIO is defined. */
\r
137 const char * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";
\r
138 const char * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";
\r
140 /*-----------------------------------------------------------*/
\r
142 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
144 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
145 const portTickType xBlockTime = ( portTickType ) 100;
\r
147 /* Create the structure used to pass parameters to the first two tasks. */
\r
148 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
150 if( pxFirstSemaphoreParameters != NULL )
\r
152 /* Create the semaphore used by the first two tasks. */
\r
153 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
155 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
157 /* Create the variable which is to be shared by the first two tasks. */
\r
158 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
160 /* Initialise the share variable to the value the tasks expect. */
\r
161 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
163 /* The first two tasks do not block on semaphore calls. */
\r
164 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
166 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
167 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
168 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
172 /* Do exactly the same to create the second set of tasks, only this time
\r
173 provide a block time for the semaphore calls. */
\r
174 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
175 if( pxSecondSemaphoreParameters != NULL )
\r
177 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
179 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
181 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
182 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
183 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
185 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
186 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
190 /*-----------------------------------------------------------*/
\r
192 static void prvSemaphoreTest( void *pvParameters )
\r
194 xSemaphoreParameters *pxParameters;
\r
195 volatile unsigned long *pulSharedVariable, ulExpectedValue;
\r
196 unsigned long ulCounter;
\r
197 short sError = pdFALSE, sCheckVariableToUse;
\r
199 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
201 portENTER_CRITICAL();
\r
202 sCheckVariableToUse = sNextCheckVariable;
\r
203 sNextCheckVariable++;
\r
204 portEXIT_CRITICAL();
\r
206 /* Queue a message for printing to say the task has started. */
\r
207 vPrintDisplayMessage( &pcSemaphoreTaskStart );
\r
209 /* A structure is passed in as the parameter. This contains the shared
\r
210 variable being guarded. */
\r
211 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
212 pulSharedVariable = pxParameters->pulSharedVariable;
\r
214 /* If we are blocking we use a much higher count to ensure loads of context
\r
215 switches occur during the count. */
\r
216 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
218 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
222 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
227 /* Try to obtain the semaphore. */
\r
228 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
230 /* We have the semaphore and so expect any other tasks using the
\r
231 shared variable to have left it in the state we expect to find
\r
233 if( *pulSharedVariable != ulExpectedValue )
\r
235 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
239 /* Clear the variable, then count it back up to the expected value
\r
240 before releasing the semaphore. Would expect a context switch or
\r
241 two during this time. */
\r
242 for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
244 *pulSharedVariable = ulCounter;
\r
245 if( *pulSharedVariable != ulCounter )
\r
247 if( sError == pdFALSE )
\r
249 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
255 /* Release the semaphore, and if no errors have occurred increment the check
\r
257 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
259 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
263 if( sError == pdFALSE )
\r
265 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
267 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
271 /* If we have a block time then we are running at a priority higher
\r
272 than the idle priority. This task takes a long time to complete
\r
273 a cycle (deliberately so to test the guarding) so will be starving
\r
274 out lower priority tasks. Block for some time to allow give lower
\r
275 priority tasks some processor time. */
\r
276 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
280 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
282 /* We have not got the semaphore yet, so no point using the
\r
283 processor. We are not blocking when attempting to obtain the
\r
290 /*-----------------------------------------------------------*/
\r
292 /* This is called to check that all the created tasks are still running. */
\r
293 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
295 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
296 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
298 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
300 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
305 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r