]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/semtest.c
f1460eb903dfba646b37f8afc8347a03369d439d
[freertos] / FreeRTOS / Demo / Common / Minimal / semtest.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
77  * to which is guarded by a semaphore.\r
78  * \r
79  * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
80  * semaphore a task checks to ensure that the guarded variable has an expected \r
81  * value.  It then clears the variable to zero before counting it back up to the \r
82  * expected value in increments of 1.  After each increment the variable is checked \r
83  * to ensure it contains the value to which it was just set. When the starting \r
84  * value is again reached the task releases the semaphore giving the other task in \r
85  * the set a chance to do exactly the same thing.  The starting value is high \r
86  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
87  *\r
88  * An error is flagged if at any time during the process a shared variable is \r
89  * found to have a value other than that expected.  Such an occurrence would \r
90  * suggest an error in the mutual exclusion mechanism by which access to the \r
91  * variable is restricted.\r
92  *\r
93  * The first set of two tasks poll their semaphore.  The second set use blocking \r
94  * calls.\r
95  *\r
96  */\r
97 \r
98 \r
99 #include <stdlib.h>\r
100 \r
101 /* Scheduler include files. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 #include "semphr.h"\r
105 \r
106 /* Demo app include files. */\r
107 #include "semtest.h"\r
108 \r
109 /* The value to which the shared variables are counted. */\r
110 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned long ) 0xfff )\r
111 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned long ) 0xff  )\r
112 \r
113 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
114 \r
115 #define semtstNUM_TASKS                         ( 4 )\r
116 \r
117 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
118 \r
119 /* The task function as described at the top of the file. */\r
120 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );\r
121 \r
122 /* Structure used to pass parameters to each task. */\r
123 typedef struct SEMAPHORE_PARAMETERS\r
124 {\r
125         xSemaphoreHandle xSemaphore;\r
126         volatile unsigned long *pulSharedVariable;\r
127         portTickType xBlockTime;\r
128 } xSemaphoreParameters;\r
129 \r
130 /* Variables used to check that all the tasks are still running without errors. */\r
131 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
132 static volatile short sNextCheckVariable = 0;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
137 {\r
138 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
139 const portTickType xBlockTime = ( portTickType ) 100;\r
140 \r
141         /* Create the structure used to pass parameters to the first two tasks. */\r
142         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
143 \r
144         if( pxFirstSemaphoreParameters != NULL )\r
145         {\r
146                 /* Create the semaphore used by the first two tasks. */\r
147                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
148 \r
149                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
150                 {\r
151                         /* Create the variable which is to be shared by the first two tasks. */\r
152                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
153 \r
154                         /* Initialise the share variable to the value the tasks expect. */\r
155                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
156 \r
157                         /* The first two tasks do not block on semaphore calls. */\r
158                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
159 \r
160                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
161                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
162                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
163                 }\r
164         }\r
165 \r
166         /* Do exactly the same to create the second set of tasks, only this time \r
167         provide a block time for the semaphore calls. */\r
168         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
169         if( pxSecondSemaphoreParameters != NULL )\r
170         {\r
171                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
172 \r
173                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
174                 {\r
175                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
176                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
177                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;\r
178 \r
179                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
180                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
181                 }\r
182         }\r
183 \r
184         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
185         in use.  The registry is provided as a means for kernel aware \r
186         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
187         is not being used.  The call to vQueueAddToRegistry() will be removed\r
188         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
189         defined to be less than 1. */\r
190         vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_1" );\r
191         vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_2" );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )\r
196 {\r
197 xSemaphoreParameters *pxParameters;\r
198 volatile unsigned long *pulSharedVariable, ulExpectedValue;\r
199 unsigned long ulCounter;\r
200 short sError = pdFALSE, sCheckVariableToUse;\r
201 \r
202         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
203         protected! */\r
204         portENTER_CRITICAL();\r
205                 sCheckVariableToUse = sNextCheckVariable;\r
206                 sNextCheckVariable++;\r
207         portEXIT_CRITICAL();\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                                 sError = pdTRUE;\r
236                         }\r
237                         \r
238                         /* Clear the variable, then count it back up to the expected value\r
239                         before releasing the semaphore.  Would expect a context switch or\r
240                         two during this time. */\r
241                         for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
242                         {\r
243                                 *pulSharedVariable = ulCounter;\r
244                                 if( *pulSharedVariable != ulCounter )\r
245                                 {\r
246                                         sError = pdTRUE;\r
247                                 }\r
248                         }\r
249 \r
250                         /* Release the semaphore, and if no errors have occurred increment the check\r
251                         variable. */\r
252                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
253                         {\r
254                                 sError = pdTRUE;\r
255                         }\r
256 \r
257                         if( sError == pdFALSE )\r
258                         {\r
259                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
260                                 {\r
261                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
262                                 }\r
263                         }\r
264 \r
265                         /* If we have a block time then we are running at a priority higher\r
266                         than the idle priority.  This task takes a long time to complete\r
267                         a cycle (deliberately so to test the guarding) so will be starving\r
268                         out lower priority tasks.  Block for some time to allow give lower\r
269                         priority tasks some processor time. */\r
270                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
271                 }\r
272                 else\r
273                 {\r
274                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
275                         {\r
276                                 /* We have not got the semaphore yet, so no point using the\r
277                                 processor.  We are not blocking when attempting to obtain the\r
278                                 semaphore. */\r
279                                 taskYIELD();\r
280                         }\r
281                 }\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 /* This is called to check that all the created tasks are still running. */\r
287 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
288 {\r
289 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
290 portBASE_TYPE xTask, xReturn = pdTRUE;\r
291 \r
292         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
293         {\r
294                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
295                 {\r
296                         xReturn = pdFALSE;\r
297                 }\r
298 \r
299                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
300         }\r
301 \r
302         return xReturn;\r
303 }\r
304 \r
305 \r