]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/semtest.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / Common / Full / semtest.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /**\r
70  * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
71  * to which is guarded by a semaphore.\r
72  * \r
73  * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
74  * semaphore a task checks to ensure that the guarded variable has an expected \r
75  * value.  It then clears the variable to zero before counting it back up to the \r
76  * expected value in increments of 1.  After each increment the variable is checked \r
77  * to ensure it contains the value to which it was just set. When the starting \r
78  * value is again reached the task releases the semaphore giving the other task in \r
79  * the set a chance to do exactly the same thing.  The starting value is high \r
80  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
81  *\r
82  * An error is flagged if at any time during the process a shared variable is \r
83  * found to have a value other than that expected.  Such an occurrence would \r
84  * suggest an error in the mutual exclusion mechanism by which access to the \r
85  * variable is restricted.\r
86  *\r
87  * The first set of two tasks poll their semaphore.  The second set use blocking \r
88  * calls.\r
89  *\r
90  * \page SemTestC semtest.c\r
91  * \ingroup DemoFiles\r
92  * <HR>\r
93  */\r
94 \r
95 /*\r
96 Changes from V1.2.0:\r
97 \r
98         + The tasks that operate at the idle priority now use a lower expected\r
99           count than those running at a higher priority.  This prevents the low\r
100           priority tasks from signaling an error because they have not been \r
101           scheduled enough time for each of them to count the shared variable to\r
102           the high value.\r
103 \r
104 Changes from V2.0.0\r
105 \r
106         + Delay periods are now specified using variables and constants of\r
107           portTickType rather than unsigned long.\r
108 \r
109 Changes from V2.1.1\r
110 \r
111         + The stack size now uses configMINIMAL_STACK_SIZE.\r
112         + String constants made file scope to decrease stack depth on 8051 port.\r
113 */\r
114 \r
115 #include <stdlib.h>\r
116 \r
117 /* Scheduler include files. */\r
118 #include "FreeRTOS.h"\r
119 #include "task.h"\r
120 #include "semphr.h"\r
121 \r
122 /* Demo app include files. */\r
123 #include "semtest.h"\r
124 #include "print.h"\r
125 \r
126 /* The value to which the shared variables are counted. */\r
127 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned long ) 0xfff )\r
128 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned long ) 0xff  )\r
129 \r
130 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
131 \r
132 #define semtstNUM_TASKS                         ( 4 )\r
133 \r
134 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
135 \r
136 /* The task function as described at the top of the file. */\r
137 static void prvSemaphoreTest( void *pvParameters );\r
138 \r
139 /* Structure used to pass parameters to each task. */\r
140 typedef struct SEMAPHORE_PARAMETERS\r
141 {\r
142         xSemaphoreHandle xSemaphore;\r
143         volatile unsigned long *pulSharedVariable;\r
144         portTickType xBlockTime;\r
145 } xSemaphoreParameters;\r
146 \r
147 /* Variables used to check that all the tasks are still running without errors. */\r
148 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
149 static volatile short sNextCheckVariable = 0;\r
150 \r
151 /* Strings to print if USE_STDIO is defined. */\r
152 const char * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";\r
153 const char * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
158 {\r
159 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
160 const portTickType xBlockTime = ( portTickType ) 100;\r
161 \r
162         /* Create the structure used to pass parameters to the first two tasks. */\r
163         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
164 \r
165         if( pxFirstSemaphoreParameters != NULL )\r
166         {\r
167                 /* Create the semaphore used by the first two tasks. */\r
168                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
169 \r
170                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
171                 {\r
172                         /* Create the variable which is to be shared by the first two tasks. */\r
173                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
174 \r
175                         /* Initialise the share variable to the value the tasks expect. */\r
176                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
177 \r
178                         /* The first two tasks do not block on semaphore calls. */\r
179                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
180 \r
181                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
182                         xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
183                         xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
184                 }\r
185         }\r
186 \r
187         /* Do exactly the same to create the second set of tasks, only this time \r
188         provide a block time for the semaphore calls. */\r
189         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
190         if( pxSecondSemaphoreParameters != NULL )\r
191         {\r
192                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
193 \r
194                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
195                 {\r
196                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
197                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
198                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;\r
199 \r
200                         xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
201                         xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
202                 }\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvSemaphoreTest( void *pvParameters )\r
208 {\r
209 xSemaphoreParameters *pxParameters;\r
210 volatile unsigned long *pulSharedVariable, ulExpectedValue;\r
211 unsigned long ulCounter;\r
212 short sError = pdFALSE, sCheckVariableToUse;\r
213 \r
214         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
215         protected! */\r
216         portENTER_CRITICAL();\r
217                 sCheckVariableToUse = sNextCheckVariable;\r
218                 sNextCheckVariable++;\r
219         portEXIT_CRITICAL();\r
220 \r
221         /* Queue a message for printing to say the task has started. */\r
222         vPrintDisplayMessage( &pcSemaphoreTaskStart );\r
223 \r
224         /* A structure is passed in as the parameter.  This contains the shared \r
225         variable being guarded. */\r
226         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
227         pulSharedVariable = pxParameters->pulSharedVariable;\r
228 \r
229         /* If we are blocking we use a much higher count to ensure loads of context\r
230         switches occur during the count. */\r
231         if( pxParameters->xBlockTime > ( portTickType ) 0 )\r
232         {\r
233                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
234         }\r
235         else\r
236         {\r
237                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
238         }\r
239 \r
240         for( ;; )\r
241         {\r
242                 /* Try to obtain the semaphore. */\r
243                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
244                 {\r
245                         /* We have the semaphore and so expect any other tasks using the\r
246                         shared variable to have left it in the state we expect to find\r
247                         it. */\r
248                         if( *pulSharedVariable != ulExpectedValue )\r
249                         {\r
250                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
251                                 sError = pdTRUE;\r
252                         }\r
253                         \r
254                         /* Clear the variable, then count it back up to the expected value\r
255                         before releasing the semaphore.  Would expect a context switch or\r
256                         two during this time. */\r
257                         for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
258                         {\r
259                                 *pulSharedVariable = ulCounter;\r
260                                 if( *pulSharedVariable != ulCounter )\r
261                                 {\r
262                                         if( sError == pdFALSE )\r
263                                         {\r
264                                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
265                                         }\r
266                                         sError = pdTRUE;\r
267                                 }\r
268                         }\r
269 \r
270                         /* Release the semaphore, and if no errors have occurred increment the check\r
271                         variable. */\r
272                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
273                         {\r
274                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
275                                 sError = pdTRUE;\r
276                         }\r
277 \r
278                         if( sError == pdFALSE )\r
279                         {\r
280                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
281                                 {\r
282                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
283                                 }\r
284                         }\r
285 \r
286                         /* If we have a block time then we are running at a priority higher\r
287                         than the idle priority.  This task takes a long time to complete\r
288                         a cycle (deliberately so to test the guarding) so will be starving\r
289                         out lower priority tasks.  Block for some time to allow give lower\r
290                         priority tasks some processor time. */\r
291                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
292                 }\r
293                 else\r
294                 {\r
295                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
296                         {\r
297                                 /* We have not got the semaphore yet, so no point using the\r
298                                 processor.  We are not blocking when attempting to obtain the\r
299                                 semaphore. */\r
300                                 taskYIELD();\r
301                         }\r
302                 }\r
303         }\r
304 }\r
305 /*-----------------------------------------------------------*/\r
306 \r
307 /* This is called to check that all the created tasks are still running. */\r
308 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
309 {\r
310 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
311 portBASE_TYPE xTask, xReturn = pdTRUE;\r
312 \r
313         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
314         {\r
315                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
316                 {\r
317                         xReturn = pdFALSE;\r
318                 }\r
319 \r
320                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
321         }\r
322 \r
323         return xReturn;\r
324 }\r
325 \r
326 \r