]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/semtest.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / Common / Full / semtest.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\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
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\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
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /**\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
57  * \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
66  *\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
71  *\r
72  * The first set of two tasks poll their semaphore.  The second set use blocking \r
73  * calls.\r
74  *\r
75  * \page SemTestC semtest.c\r
76  * \ingroup DemoFiles\r
77  * <HR>\r
78  */\r
79 \r
80 /*\r
81 Changes from V1.2.0:\r
82 \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
87           the high value.\r
88 \r
89 Changes from V2.0.0\r
90 \r
91         + Delay periods are now specified using variables and constants of\r
92           portTickType rather than unsigned long.\r
93 \r
94 Changes from V2.1.1\r
95 \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
98 */\r
99 \r
100 #include <stdlib.h>\r
101 \r
102 /* Scheduler include files. */\r
103 #include "FreeRTOS.h"\r
104 #include "task.h"\r
105 #include "semphr.h"\r
106 \r
107 /* Demo app include files. */\r
108 #include "semtest.h"\r
109 #include "print.h"\r
110 \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
114 \r
115 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
116 \r
117 #define semtstNUM_TASKS                         ( 4 )\r
118 \r
119 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
120 \r
121 /* The task function as described at the top of the file. */\r
122 static void prvSemaphoreTest( void *pvParameters );\r
123 \r
124 /* Structure used to pass parameters to each task. */\r
125 typedef struct SEMAPHORE_PARAMETERS\r
126 {\r
127         xSemaphoreHandle xSemaphore;\r
128         volatile unsigned long *pulSharedVariable;\r
129         portTickType xBlockTime;\r
130 } xSemaphoreParameters;\r
131 \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
135 \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
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
143 {\r
144 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
145 const portTickType xBlockTime = ( portTickType ) 100;\r
146 \r
147         /* Create the structure used to pass parameters to the first two tasks. */\r
148         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
149 \r
150         if( pxFirstSemaphoreParameters != NULL )\r
151         {\r
152                 /* Create the semaphore used by the first two tasks. */\r
153                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
154 \r
155                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
156                 {\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
159 \r
160                         /* Initialise the share variable to the value the tasks expect. */\r
161                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
162 \r
163                         /* The first two tasks do not block on semaphore calls. */\r
164                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
165 \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
169                 }\r
170         }\r
171 \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
176         {\r
177                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
178 \r
179                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
180                 {\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
184 \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
187                 }\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void prvSemaphoreTest( void *pvParameters )\r
193 {\r
194 xSemaphoreParameters *pxParameters;\r
195 volatile unsigned long *pulSharedVariable, ulExpectedValue;\r
196 unsigned long ulCounter;\r
197 short sError = pdFALSE, sCheckVariableToUse;\r
198 \r
199         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
200         protected! */\r
201         portENTER_CRITICAL();\r
202                 sCheckVariableToUse = sNextCheckVariable;\r
203                 sNextCheckVariable++;\r
204         portEXIT_CRITICAL();\r
205 \r
206         /* Queue a message for printing to say the task has started. */\r
207         vPrintDisplayMessage( &pcSemaphoreTaskStart );\r
208 \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
213 \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
217         {\r
218                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
219         }\r
220         else\r
221         {\r
222                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
223         }\r
224 \r
225         for( ;; )\r
226         {\r
227                 /* Try to obtain the semaphore. */\r
228                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
229                 {\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
232                         it. */\r
233                         if( *pulSharedVariable != ulExpectedValue )\r
234                         {\r
235                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
236                                 sError = pdTRUE;\r
237                         }\r
238                         \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
243                         {\r
244                                 *pulSharedVariable = ulCounter;\r
245                                 if( *pulSharedVariable != ulCounter )\r
246                                 {\r
247                                         if( sError == pdFALSE )\r
248                                         {\r
249                                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
250                                         }\r
251                                         sError = pdTRUE;\r
252                                 }\r
253                         }\r
254 \r
255                         /* Release the semaphore, and if no errors have occurred increment the check\r
256                         variable. */\r
257                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
258                         {\r
259                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
260                                 sError = pdTRUE;\r
261                         }\r
262 \r
263                         if( sError == pdFALSE )\r
264                         {\r
265                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
266                                 {\r
267                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
268                                 }\r
269                         }\r
270 \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
277                 }\r
278                 else\r
279                 {\r
280                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
281                         {\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
284                                 semaphore. */\r
285                                 taskYIELD();\r
286                         }\r
287                 }\r
288         }\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 /* This is called to check that all the created tasks are still running. */\r
293 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
294 {\r
295 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
296 portBASE_TYPE xTask, xReturn = pdTRUE;\r
297 \r
298         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
299         {\r
300                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
301                 {\r
302                         xReturn = pdFALSE;\r
303                 }\r
304 \r
305                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
306         }\r
307 \r
308         return xReturn;\r
309 }\r
310 \r
311 \r