]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/semtest.c
37d0f9a6bce2ef61786e9e716852c86d253ba027
[freertos] / FreeRTOS / Demo / Common / Minimal / semtest.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97  * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
98  * to which is guarded by a semaphore.\r
99  * \r
100  * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
101  * semaphore a task checks to ensure that the guarded variable has an expected \r
102  * value.  It then clears the variable to zero before counting it back up to the \r
103  * expected value in increments of 1.  After each increment the variable is checked \r
104  * to ensure it contains the value to which it was just set. When the starting \r
105  * value is again reached the task releases the semaphore giving the other task in \r
106  * the set a chance to do exactly the same thing.  The starting value is high \r
107  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
108  *\r
109  * An error is flagged if at any time during the process a shared variable is \r
110  * found to have a value other than that expected.  Such an occurrence would \r
111  * suggest an error in the mutual exclusion mechanism by which access to the \r
112  * variable is restricted.\r
113  *\r
114  * The first set of two tasks poll their semaphore.  The second set use blocking \r
115  * calls.\r
116  *\r
117  */\r
118 \r
119 \r
120 #include <stdlib.h>\r
121 \r
122 /* Scheduler include files. */\r
123 #include "FreeRTOS.h"\r
124 #include "task.h"\r
125 #include "semphr.h"\r
126 \r
127 /* Demo app include files. */\r
128 #include "semtest.h"\r
129 \r
130 /* The value to which the shared variables are counted. */\r
131 #define semtstBLOCKING_EXPECTED_VALUE           ( ( uint32_t ) 0xfff )\r
132 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( uint32_t ) 0xff  )\r
133 \r
134 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
135 \r
136 #define semtstNUM_TASKS                         ( 4 )\r
137 \r
138 #define semtstDELAY_FACTOR                      ( ( TickType_t ) 10 )\r
139 \r
140 /* The task function as described at the top of the file. */\r
141 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );\r
142 \r
143 /* Structure used to pass parameters to each task. */\r
144 typedef struct SEMAPHORE_PARAMETERS\r
145 {\r
146         SemaphoreHandle_t xSemaphore;\r
147         volatile uint32_t *pulSharedVariable;\r
148         TickType_t xBlockTime;\r
149 } xSemaphoreParameters;\r
150 \r
151 /* Variables used to check that all the tasks are still running without errors. */\r
152 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
153 static volatile short sNextCheckVariable = 0;\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 void vStartSemaphoreTasks( UBaseType_t uxPriority )\r
158 {\r
159 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
160 const TickType_t xBlockTime = ( TickType_t ) 100;\r
161 \r
162         /* Create the structure used to pass parameters to the first two tasks. */\r
163         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
164 \r
165         if( pxFirstSemaphoreParameters != NULL )\r
166         {\r
167                 /* Create the semaphore used by the first two tasks. */\r
168                 pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();\r
169                 xSemaphoreGive( 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 = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );\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                 pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();\r
194                 xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );\r
195 \r
196                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
197                 {\r
198                         pxSecondSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );\r
199                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
200                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;\r
201 \r
202                         xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
203                         xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
204                 }\r
205         }\r
206 \r
207         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
208         in use.  The registry is provided as a means for kernel aware \r
209         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
210         is not being used.  The call to vQueueAddToRegistry() will be removed\r
211         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
212         defined to be less than 1. */\r
213         vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );\r
214         vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )\r
219 {\r
220 xSemaphoreParameters *pxParameters;\r
221 volatile uint32_t *pulSharedVariable, ulExpectedValue;\r
222 uint32_t ulCounter;\r
223 short sError = pdFALSE, sCheckVariableToUse;\r
224 \r
225         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
226         protected! */\r
227         portENTER_CRITICAL();\r
228                 sCheckVariableToUse = sNextCheckVariable;\r
229                 sNextCheckVariable++;\r
230         portEXIT_CRITICAL();\r
231 \r
232         /* A structure is passed in as the parameter.  This contains the shared \r
233         variable being guarded. */\r
234         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
235         pulSharedVariable = pxParameters->pulSharedVariable;\r
236 \r
237         /* If we are blocking we use a much higher count to ensure loads of context\r
238         switches occur during the count. */\r
239         if( pxParameters->xBlockTime > ( TickType_t ) 0 )\r
240         {\r
241                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
242         }\r
243         else\r
244         {\r
245                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
246         }\r
247 \r
248         for( ;; )\r
249         {\r
250                 /* Try to obtain the semaphore. */\r
251                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
252                 {\r
253                         /* We have the semaphore and so expect any other tasks using the\r
254                         shared variable to have left it in the state we expect to find\r
255                         it. */\r
256                         if( *pulSharedVariable != ulExpectedValue )\r
257                         {\r
258                                 sError = pdTRUE;\r
259                         }\r
260                         \r
261                         /* Clear the variable, then count it back up to the expected value\r
262                         before releasing the semaphore.  Would expect a context switch or\r
263                         two during this time. */\r
264                         for( ulCounter = ( uint32_t ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
265                         {\r
266                                 *pulSharedVariable = ulCounter;\r
267                                 if( *pulSharedVariable != ulCounter )\r
268                                 {\r
269                                         sError = pdTRUE;\r
270                                 }\r
271                         }\r
272 \r
273                         /* Release the semaphore, and if no errors have occurred increment the check\r
274                         variable. */\r
275                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
276                         {\r
277                                 sError = pdTRUE;\r
278                         }\r
279 \r
280                         if( sError == pdFALSE )\r
281                         {\r
282                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
283                                 {\r
284                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
285                                 }\r
286                         }\r
287 \r
288                         /* If we have a block time then we are running at a priority higher\r
289                         than the idle priority.  This task takes a long time to complete\r
290                         a cycle (deliberately so to test the guarding) so will be starving\r
291                         out lower priority tasks.  Block for some time to allow give lower\r
292                         priority tasks some processor time. */\r
293                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
294                 }\r
295                 else\r
296                 {\r
297                         if( pxParameters->xBlockTime == ( TickType_t ) 0 )\r
298                         {\r
299                                 /* We have not got the semaphore yet, so no point using the\r
300                                 processor.  We are not blocking when attempting to obtain the\r
301                                 semaphore. */\r
302                                 taskYIELD();\r
303                         }\r
304                 }\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 /* This is called to check that all the created tasks are still running. */\r
310 BaseType_t xAreSemaphoreTasksStillRunning( void )\r
311 {\r
312 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
313 BaseType_t xTask, xReturn = pdTRUE;\r
314 \r
315         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
316         {\r
317                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
318                 {\r
319                         xReturn = pdFALSE;\r
320                 }\r
321 \r
322                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
323         }\r
324 \r
325         return xReturn;\r
326 }\r
327 \r
328 \r