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