]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/semtest.c
Update version number in preparation for official V8.2.0 release.
[freertos] / FreeRTOS / Demo / Common / Minimal / 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  */\r
92 \r
93 \r
94 #include <stdlib.h>\r
95 \r
96 /* Scheduler include files. */\r
97 #include "FreeRTOS.h"\r
98 #include "task.h"\r
99 #include "semphr.h"\r
100 \r
101 /* Demo app include files. */\r
102 #include "semtest.h"\r
103 \r
104 /* The value to which the shared variables are counted. */\r
105 #define semtstBLOCKING_EXPECTED_VALUE           ( ( uint32_t ) 0xfff )\r
106 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( uint32_t ) 0xff  )\r
107 \r
108 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
109 \r
110 #define semtstNUM_TASKS                         ( 4 )\r
111 \r
112 #define semtstDELAY_FACTOR                      ( ( TickType_t ) 10 )\r
113 \r
114 /* The task function as described at the top of the file. */\r
115 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );\r
116 \r
117 /* Structure used to pass parameters to each task. */\r
118 typedef struct SEMAPHORE_PARAMETERS\r
119 {\r
120         SemaphoreHandle_t xSemaphore;\r
121         volatile uint32_t *pulSharedVariable;\r
122         TickType_t xBlockTime;\r
123 } xSemaphoreParameters;\r
124 \r
125 /* Variables used to check that all the tasks are still running without errors. */\r
126 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
127 static volatile short sNextCheckVariable = 0;\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 void vStartSemaphoreTasks( UBaseType_t uxPriority )\r
132 {\r
133 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
134 const TickType_t xBlockTime = ( TickType_t ) 100;\r
135 \r
136         /* Create the structure used to pass parameters to the first two tasks. */\r
137         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
138 \r
139         if( pxFirstSemaphoreParameters != NULL )\r
140         {\r
141                 /* Create the semaphore used by the first two tasks. */\r
142                 pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();\r
143                 xSemaphoreGive( pxFirstSemaphoreParameters->xSemaphore );\r
144 \r
145                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
146                 {\r
147                         /* Create the variable which is to be shared by the first two tasks. */\r
148                         pxFirstSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );\r
149 \r
150                         /* Initialise the share variable to the value the tasks expect. */\r
151                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
152 \r
153                         /* The first two tasks do not block on semaphore calls. */\r
154                         pxFirstSemaphoreParameters->xBlockTime = ( TickType_t ) 0;\r
155 \r
156                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
157                         xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );\r
158                         xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );\r
159                 }\r
160         }\r
161 \r
162         /* Do exactly the same to create the second set of tasks, only this time \r
163         provide a block time for the semaphore calls. */\r
164         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
165         if( pxSecondSemaphoreParameters != NULL )\r
166         {\r
167                 pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();\r
168                 xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );\r
169 \r
170                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
171                 {\r
172                         pxSecondSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );\r
173                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
174                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;\r
175 \r
176                         xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
177                         xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
178                 }\r
179         }\r
180 \r
181         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
182         in use.  The registry is provided as a means for kernel aware \r
183         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
184         is not being used.  The call to vQueueAddToRegistry() will be removed\r
185         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
186         defined to be less than 1. */\r
187         vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );\r
188         vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )\r
193 {\r
194 xSemaphoreParameters *pxParameters;\r
195 volatile uint32_t *pulSharedVariable, ulExpectedValue;\r
196 uint32_t 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         /* A structure is passed in as the parameter.  This contains the shared \r
207         variable being guarded. */\r
208         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
209         pulSharedVariable = pxParameters->pulSharedVariable;\r
210 \r
211         /* If we are blocking we use a much higher count to ensure loads of context\r
212         switches occur during the count. */\r
213         if( pxParameters->xBlockTime > ( TickType_t ) 0 )\r
214         {\r
215                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
216         }\r
217         else\r
218         {\r
219                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
220         }\r
221 \r
222         for( ;; )\r
223         {\r
224                 /* Try to obtain the semaphore. */\r
225                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
226                 {\r
227                         /* We have the semaphore and so expect any other tasks using the\r
228                         shared variable to have left it in the state we expect to find\r
229                         it. */\r
230                         if( *pulSharedVariable != ulExpectedValue )\r
231                         {\r
232                                 sError = pdTRUE;\r
233                         }\r
234                         \r
235                         /* Clear the variable, then count it back up to the expected value\r
236                         before releasing the semaphore.  Would expect a context switch or\r
237                         two during this time. */\r
238                         for( ulCounter = ( uint32_t ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
239                         {\r
240                                 *pulSharedVariable = ulCounter;\r
241                                 if( *pulSharedVariable != ulCounter )\r
242                                 {\r
243                                         sError = pdTRUE;\r
244                                 }\r
245                         }\r
246 \r
247                         /* Release the semaphore, and if no errors have occurred increment the check\r
248                         variable. */\r
249                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
250                         {\r
251                                 sError = pdTRUE;\r
252                         }\r
253 \r
254                         if( sError == pdFALSE )\r
255                         {\r
256                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
257                                 {\r
258                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
259                                 }\r
260                         }\r
261 \r
262                         /* If we have a block time then we are running at a priority higher\r
263                         than the idle priority.  This task takes a long time to complete\r
264                         a cycle (deliberately so to test the guarding) so will be starving\r
265                         out lower priority tasks.  Block for some time to allow give lower\r
266                         priority tasks some processor time. */\r
267                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
268                 }\r
269                 else\r
270                 {\r
271                         if( pxParameters->xBlockTime == ( TickType_t ) 0 )\r
272                         {\r
273                                 /* We have not got the semaphore yet, so no point using the\r
274                                 processor.  We are not blocking when attempting to obtain the\r
275                                 semaphore. */\r
276                                 taskYIELD();\r
277                         }\r
278                 }\r
279         }\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 /* This is called to check that all the created tasks are still running. */\r
284 BaseType_t xAreSemaphoreTasksStillRunning( void )\r
285 {\r
286 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
287 BaseType_t xTask, xReturn = pdTRUE;\r
288 \r
289         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
290         {\r
291                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
292                 {\r
293                         xReturn = pdFALSE;\r
294                 }\r
295 \r
296                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
297         }\r
298 \r
299         return xReturn;\r
300 }\r
301 \r
302 \r