]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/semtest.c
Correct task names in BlockQ.c.
[freertos] / Demo / Common / Minimal / semtest.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\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
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 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
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  */\r
76 \r
77 \r
78 #include <stdlib.h>\r
79 \r
80 /* Scheduler include files. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "semphr.h"\r
84 \r
85 /* Demo app include files. */\r
86 #include "semtest.h"\r
87 \r
88 /* The value to which the shared variables are counted. */\r
89 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned long ) 0xfff )\r
90 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned long ) 0xff  )\r
91 \r
92 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
93 \r
94 #define semtstNUM_TASKS                         ( 4 )\r
95 \r
96 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
97 \r
98 /* The task function as described at the top of the file. */\r
99 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );\r
100 \r
101 /* Structure used to pass parameters to each task. */\r
102 typedef struct SEMAPHORE_PARAMETERS\r
103 {\r
104         xSemaphoreHandle xSemaphore;\r
105         volatile unsigned long *pulSharedVariable;\r
106         portTickType xBlockTime;\r
107 } xSemaphoreParameters;\r
108 \r
109 /* Variables used to check that all the tasks are still running without errors. */\r
110 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
111 static volatile short sNextCheckVariable = 0;\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
116 {\r
117 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
118 const portTickType xBlockTime = ( portTickType ) 100;\r
119 \r
120         /* Create the structure used to pass parameters to the first two tasks. */\r
121         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
122 \r
123         if( pxFirstSemaphoreParameters != NULL )\r
124         {\r
125                 /* Create the semaphore used by the first two tasks. */\r
126                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
127 \r
128                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
129                 {\r
130                         /* Create the variable which is to be shared by the first two tasks. */\r
131                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
132 \r
133                         /* Initialise the share variable to the value the tasks expect. */\r
134                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
135 \r
136                         /* The first two tasks do not block on semaphore calls. */\r
137                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
138 \r
139                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
140                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
141                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
142                 }\r
143         }\r
144 \r
145         /* Do exactly the same to create the second set of tasks, only this time \r
146         provide a block time for the semaphore calls. */\r
147         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
148         if( pxSecondSemaphoreParameters != NULL )\r
149         {\r
150                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
151 \r
152                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
153                 {\r
154                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
155                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
156                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;\r
157 \r
158                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
159                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
160                 }\r
161         }\r
162 \r
163         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
164         in use.  The registry is provided as a means for kernel aware \r
165         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
166         is not being used.  The call to vQueueAddToRegistry() will be removed\r
167         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
168         defined to be less than 1. */\r
169         vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_1" );\r
170         vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_2" );\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )\r
175 {\r
176 xSemaphoreParameters *pxParameters;\r
177 volatile unsigned long *pulSharedVariable, ulExpectedValue;\r
178 unsigned long ulCounter;\r
179 short sError = pdFALSE, sCheckVariableToUse;\r
180 \r
181         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
182         protected! */\r
183         portENTER_CRITICAL();\r
184                 sCheckVariableToUse = sNextCheckVariable;\r
185                 sNextCheckVariable++;\r
186         portEXIT_CRITICAL();\r
187 \r
188         /* A structure is passed in as the parameter.  This contains the shared \r
189         variable being guarded. */\r
190         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
191         pulSharedVariable = pxParameters->pulSharedVariable;\r
192 \r
193         /* If we are blocking we use a much higher count to ensure loads of context\r
194         switches occur during the count. */\r
195         if( pxParameters->xBlockTime > ( portTickType ) 0 )\r
196         {\r
197                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
198         }\r
199         else\r
200         {\r
201                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
202         }\r
203 \r
204         for( ;; )\r
205         {\r
206                 /* Try to obtain the semaphore. */\r
207                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
208                 {\r
209                         /* We have the semaphore and so expect any other tasks using the\r
210                         shared variable to have left it in the state we expect to find\r
211                         it. */\r
212                         if( *pulSharedVariable != ulExpectedValue )\r
213                         {\r
214                                 sError = pdTRUE;\r
215                         }\r
216                         \r
217                         /* Clear the variable, then count it back up to the expected value\r
218                         before releasing the semaphore.  Would expect a context switch or\r
219                         two during this time. */\r
220                         for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
221                         {\r
222                                 *pulSharedVariable = ulCounter;\r
223                                 if( *pulSharedVariable != ulCounter )\r
224                                 {\r
225                                         sError = pdTRUE;\r
226                                 }\r
227                         }\r
228 \r
229                         /* Release the semaphore, and if no errors have occurred increment the check\r
230                         variable. */\r
231                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
232                         {\r
233                                 sError = pdTRUE;\r
234                         }\r
235 \r
236                         if( sError == pdFALSE )\r
237                         {\r
238                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
239                                 {\r
240                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
241                                 }\r
242                         }\r
243 \r
244                         /* If we have a block time then we are running at a priority higher\r
245                         than the idle priority.  This task takes a long time to complete\r
246                         a cycle (deliberately so to test the guarding) so will be starving\r
247                         out lower priority tasks.  Block for some time to allow give lower\r
248                         priority tasks some processor time. */\r
249                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
250                 }\r
251                 else\r
252                 {\r
253                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
254                         {\r
255                                 /* We have not got the semaphore yet, so no point using the\r
256                                 processor.  We are not blocking when attempting to obtain the\r
257                                 semaphore. */\r
258                                 taskYIELD();\r
259                         }\r
260                 }\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 /* This is called to check that all the created tasks are still running. */\r
266 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
267 {\r
268 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
269 portBASE_TYPE xTask, xReturn = pdTRUE;\r
270 \r
271         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
272         {\r
273                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
274                 {\r
275                         xReturn = pdFALSE;\r
276                 }\r
277 \r
278                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
279         }\r
280 \r
281         return xReturn;\r
282 }\r
283 \r
284 \r