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