2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
67 * to which is guarded by a semaphore.
\r
69 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
70 * semaphore a task checks to ensure that the guarded variable has an expected
\r
71 * value. It then clears the variable to zero before counting it back up to the
\r
72 * expected value in increments of 1. After each increment the variable is checked
\r
73 * to ensure it contains the value to which it was just set. When the starting
\r
74 * value is again reached the task releases the semaphore giving the other task in
\r
75 * the set a chance to do exactly the same thing. The starting value is high
\r
76 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
78 * An error is flagged if at any time during the process a shared variable is
\r
79 * found to have a value other than that expected. Such an occurrence would
\r
80 * suggest an error in the mutual exclusion mechanism by which access to the
\r
81 * variable is restricted.
\r
83 * The first set of two tasks poll their semaphore. The second set use blocking
\r
86 * \page SemTestC semtest.c
\r
87 * \ingroup DemoFiles
\r
92 Changes from V1.2.0:
\r
94 + The tasks that operate at the idle priority now use a lower expected
\r
95 count than those running at a higher priority. This prevents the low
\r
96 priority tasks from signaling an error because they have not been
\r
97 scheduled enough time for each of them to count the shared variable to
\r
100 Changes from V2.0.0
\r
102 + Delay periods are now specified using variables and constants of
\r
103 portTickType rather than unsigned long.
\r
105 Changes from V2.1.1
\r
107 + The stack size now uses configMINIMAL_STACK_SIZE.
\r
108 + String constants made file scope to decrease stack depth on 8051 port.
\r
111 #include <stdlib.h>
\r
113 /* Scheduler include files. */
\r
114 #include "FreeRTOS.h"
\r
116 #include "semphr.h"
\r
118 /* Demo app include files. */
\r
119 #include "semtest.h"
\r
122 /* The value to which the shared variables are counted. */
\r
123 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xfff )
\r
124 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xff )
\r
126 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
128 #define semtstNUM_TASKS ( 4 )
\r
130 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
132 /* The task function as described at the top of the file. */
\r
133 static void prvSemaphoreTest( void *pvParameters );
\r
135 /* Structure used to pass parameters to each task. */
\r
136 typedef struct SEMAPHORE_PARAMETERS
\r
138 xSemaphoreHandle xSemaphore;
\r
139 volatile unsigned long *pulSharedVariable;
\r
140 portTickType xBlockTime;
\r
141 } xSemaphoreParameters;
\r
143 /* Variables used to check that all the tasks are still running without errors. */
\r
144 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
145 static volatile short sNextCheckVariable = 0;
\r
147 /* Strings to print if USE_STDIO is defined. */
\r
148 const char * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";
\r
149 const char * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";
\r
151 /*-----------------------------------------------------------*/
\r
153 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
155 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
156 const portTickType xBlockTime = ( portTickType ) 100;
\r
158 /* Create the structure used to pass parameters to the first two tasks. */
\r
159 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
161 if( pxFirstSemaphoreParameters != NULL )
\r
163 /* Create the semaphore used by the first two tasks. */
\r
164 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
166 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
168 /* Create the variable which is to be shared by the first two tasks. */
\r
169 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
171 /* Initialise the share variable to the value the tasks expect. */
\r
172 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
174 /* The first two tasks do not block on semaphore calls. */
\r
175 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
177 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
178 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
179 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
183 /* Do exactly the same to create the second set of tasks, only this time
\r
184 provide a block time for the semaphore calls. */
\r
185 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
186 if( pxSecondSemaphoreParameters != NULL )
\r
188 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
190 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
192 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
193 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
194 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
196 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
197 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
201 /*-----------------------------------------------------------*/
\r
203 static void prvSemaphoreTest( void *pvParameters )
\r
205 xSemaphoreParameters *pxParameters;
\r
206 volatile unsigned long *pulSharedVariable, ulExpectedValue;
\r
207 unsigned long ulCounter;
\r
208 short sError = pdFALSE, sCheckVariableToUse;
\r
210 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
212 portENTER_CRITICAL();
\r
213 sCheckVariableToUse = sNextCheckVariable;
\r
214 sNextCheckVariable++;
\r
215 portEXIT_CRITICAL();
\r
217 /* Queue a message for printing to say the task has started. */
\r
218 vPrintDisplayMessage( &pcSemaphoreTaskStart );
\r
220 /* A structure is passed in as the parameter. This contains the shared
\r
221 variable being guarded. */
\r
222 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
223 pulSharedVariable = pxParameters->pulSharedVariable;
\r
225 /* If we are blocking we use a much higher count to ensure loads of context
\r
226 switches occur during the count. */
\r
227 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
229 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
233 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
238 /* Try to obtain the semaphore. */
\r
239 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
241 /* We have the semaphore and so expect any other tasks using the
\r
242 shared variable to have left it in the state we expect to find
\r
244 if( *pulSharedVariable != ulExpectedValue )
\r
246 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
250 /* Clear the variable, then count it back up to the expected value
\r
251 before releasing the semaphore. Would expect a context switch or
\r
252 two during this time. */
\r
253 for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
255 *pulSharedVariable = ulCounter;
\r
256 if( *pulSharedVariable != ulCounter )
\r
258 if( sError == pdFALSE )
\r
260 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
266 /* Release the semaphore, and if no errors have occurred increment the check
\r
268 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
270 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
274 if( sError == pdFALSE )
\r
276 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
278 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
282 /* If we have a block time then we are running at a priority higher
\r
283 than the idle priority. This task takes a long time to complete
\r
284 a cycle (deliberately so to test the guarding) so will be starving
\r
285 out lower priority tasks. Block for some time to allow give lower
\r
286 priority tasks some processor time. */
\r
287 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
291 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
293 /* We have not got the semaphore yet, so no point using the
\r
294 processor. We are not blocking when attempting to obtain the
\r
301 /*-----------------------------------------------------------*/
\r
303 /* This is called to check that all the created tasks are still running. */
\r
304 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
306 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
307 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
309 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
311 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
316 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r