2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
69 * to which is guarded by a semaphore.
\r
71 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
72 * semaphore a task checks to ensure that the guarded variable has an expected
\r
73 * value. It then clears the variable to zero before counting it back up to the
\r
74 * expected value in increments of 1. After each increment the variable is checked
\r
75 * to ensure it contains the value to which it was just set. When the starting
\r
76 * value is again reached the task releases the semaphore giving the other task in
\r
77 * the set a chance to do exactly the same thing. The starting value is high
\r
78 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
80 * An error is flagged if at any time during the process a shared variable is
\r
81 * found to have a value other than that expected. Such an occurrence would
\r
82 * suggest an error in the mutual exclusion mechanism by which access to the
\r
83 * variable is restricted.
\r
85 * The first set of two tasks poll their semaphore. The second set use blocking
\r
93 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
98 /* Demo app include files. */
\r
99 #include "semtest.h"
\r
101 /* The value to which the shared variables are counted. */
\r
102 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xfff )
\r
103 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xff )
\r
105 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
107 #define semtstNUM_TASKS ( 4 )
\r
109 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
111 /* The task function as described at the top of the file. */
\r
112 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
\r
114 /* Structure used to pass parameters to each task. */
\r
115 typedef struct SEMAPHORE_PARAMETERS
\r
117 xSemaphoreHandle xSemaphore;
\r
118 volatile unsigned long *pulSharedVariable;
\r
119 portTickType xBlockTime;
\r
120 } xSemaphoreParameters;
\r
122 /* Variables used to check that all the tasks are still running without errors. */
\r
123 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
124 static volatile short sNextCheckVariable = 0;
\r
126 /*-----------------------------------------------------------*/
\r
128 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
130 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
131 const portTickType xBlockTime = ( portTickType ) 100;
\r
133 /* Create the structure used to pass parameters to the first two tasks. */
\r
134 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
136 if( pxFirstSemaphoreParameters != NULL )
\r
138 /* Create the semaphore used by the first two tasks. */
\r
139 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
141 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
143 /* Create the variable which is to be shared by the first two tasks. */
\r
144 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
146 /* Initialise the share variable to the value the tasks expect. */
\r
147 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
149 /* The first two tasks do not block on semaphore calls. */
\r
150 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
152 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
153 xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
154 xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
158 /* Do exactly the same to create the second set of tasks, only this time
\r
159 provide a block time for the semaphore calls. */
\r
160 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
161 if( pxSecondSemaphoreParameters != NULL )
\r
163 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
165 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
167 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
168 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
169 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
171 xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
172 xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
176 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
177 in use. The registry is provided as a means for kernel aware
\r
178 debuggers to locate semaphores and has no purpose if a kernel aware debugger
\r
179 is not being used. The call to vQueueAddToRegistry() will be removed
\r
180 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
181 defined to be less than 1. */
\r
182 vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_1" );
\r
183 vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_2" );
\r
185 /*-----------------------------------------------------------*/
\r
187 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
\r
189 xSemaphoreParameters *pxParameters;
\r
190 volatile unsigned long *pulSharedVariable, ulExpectedValue;
\r
191 unsigned long ulCounter;
\r
192 short sError = pdFALSE, sCheckVariableToUse;
\r
194 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
196 portENTER_CRITICAL();
\r
197 sCheckVariableToUse = sNextCheckVariable;
\r
198 sNextCheckVariable++;
\r
199 portEXIT_CRITICAL();
\r
201 /* A structure is passed in as the parameter. This contains the shared
\r
202 variable being guarded. */
\r
203 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
204 pulSharedVariable = pxParameters->pulSharedVariable;
\r
206 /* If we are blocking we use a much higher count to ensure loads of context
\r
207 switches occur during the count. */
\r
208 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
210 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
214 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
219 /* Try to obtain the semaphore. */
\r
220 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
222 /* We have the semaphore and so expect any other tasks using the
\r
223 shared variable to have left it in the state we expect to find
\r
225 if( *pulSharedVariable != ulExpectedValue )
\r
230 /* Clear the variable, then count it back up to the expected value
\r
231 before releasing the semaphore. Would expect a context switch or
\r
232 two during this time. */
\r
233 for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
235 *pulSharedVariable = ulCounter;
\r
236 if( *pulSharedVariable != ulCounter )
\r
242 /* Release the semaphore, and if no errors have occurred increment the check
\r
244 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
249 if( sError == pdFALSE )
\r
251 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
253 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
257 /* If we have a block time then we are running at a priority higher
\r
258 than the idle priority. This task takes a long time to complete
\r
259 a cycle (deliberately so to test the guarding) so will be starving
\r
260 out lower priority tasks. Block for some time to allow give lower
\r
261 priority tasks some processor time. */
\r
262 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
266 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
268 /* We have not got the semaphore yet, so no point using the
\r
269 processor. We are not blocking when attempting to obtain the
\r
276 /*-----------------------------------------------------------*/
\r
278 /* This is called to check that all the created tasks are still running. */
\r
279 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
281 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
282 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
284 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
286 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
291 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r