]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/countsem.c
0d20e8b78981d5d494c5bf61166c2805c50c7c8d
[freertos] / Demo / Common / Minimal / countsem.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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 /* \r
56  * Simple demonstration of the usage of counting semaphore.\r
57  */\r
58 \r
59 /* Scheduler include files. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 #include "semphr.h"\r
63 \r
64 /* Demo program include files. */\r
65 #include "countsem.h"\r
66 \r
67 /* The maximum count value that the semaphore used for the demo can hold. */\r
68 #define countMAX_COUNT_VALUE    ( 200 )\r
69 \r
70 /* Constants used to indicate whether or not the semaphore should have been\r
71 created with its maximum count value, or its minimum count value.  These \r
72 numbers are used to ensure that the pointers passed in as the task parameters\r
73 are valid. */\r
74 #define countSTART_AT_MAX_COUNT ( 0xaa )\r
75 #define countSTART_AT_ZERO              ( 0x55 )\r
76 \r
77 /* Two tasks are created for the test.  One uses a semaphore created with its\r
78 count value set to the maximum, and one with the count value set to zero. */\r
79 #define countNUM_TEST_TASKS             ( 2 )\r
80 #define countDONT_BLOCK                 ( 0 )\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
85 detected in any of the tasks. */\r
86 static volatile portBASE_TYPE xErrorDetected = pdFALSE;\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /*\r
91  * The demo task.  This simply counts the semaphore up to its maximum value,\r
92  * the counts it back down again.  The result of each semaphore 'give' and\r
93  * 'take' is inspected, with an error being flagged if it is found not to be\r
94  * the expected result.\r
95  */\r
96 static void prvCountingSemaphoreTask( void *pvParameters );\r
97 \r
98 /*\r
99  * Utility function to increment the semaphore count value up from zero to\r
100  * countMAX_COUNT_VALUE.\r
101  */\r
102 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
103 \r
104 /*\r
105  * Utility function to decrement the semaphore count value up from \r
106  * countMAX_COUNT_VALUE to zero.\r
107  */\r
108 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* The structure that is passed into the task as the task parameter. */\r
113 typedef struct COUNT_SEM_STRUCT\r
114 {\r
115         /* The semaphore to be used for the demo. */\r
116         xSemaphoreHandle xSemaphore;\r
117 \r
118         /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with\r
119         its count value set to its max count value, or countSTART_AT_ZERO if it\r
120         should have been created with its count value set to 0. */\r
121         unsigned portBASE_TYPE uxExpectedStartCount;    \r
122 \r
123         /* Incremented on each cycle of the demo task.  Used to detect a stalled\r
124         task. */\r
125         unsigned portBASE_TYPE uxLoopCounter;                   \r
126 } xCountSemStruct;\r
127 \r
128 /* Two structures are defined, one is passed to each test task. */\r
129 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 void vStartCountingSemaphoreTasks( void )\r
134 {\r
135         /* Create the semaphores that we are going to use for the test/demo.  The\r
136         first should be created such that it starts at its maximum count value,\r
137         the second should be created such that it starts with a count value of zero. */\r
138         xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );\r
139         xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;\r
140         xParameters[ 0 ].uxLoopCounter = 0;\r
141 \r
142         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );\r
143         xParameters[ 1 ].uxExpectedStartCount = 0;\r
144         xParameters[ 1 ].uxLoopCounter = 0;\r
145 \r
146         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
147         in use.  The registry is provided as a means for kernel aware \r
148         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
149         is not being used.  The call to vQueueAddToRegistry() will be removed\r
150         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
151         defined to be less than 1. */\r
152         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );\r
153         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );\r
154 \r
155 \r
156         /* Were the semaphores created? */\r
157         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )\r
158         {\r
159                 /* Create the demo tasks, passing in the semaphore to use as the parameter. */\r
160                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );\r
161                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );              \r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
167 {\r
168 unsigned portBASE_TYPE ux;\r
169 \r
170         /* If the semaphore count is at its maximum then we should not be able to\r
171         'give' the semaphore. */\r
172         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
173         {\r
174                 xErrorDetected = pdTRUE;\r
175         }\r
176 \r
177         /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */\r
178         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
179         {\r
180                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )\r
181                 {\r
182                         /* We expected to be able to take the semaphore. */\r
183                         xErrorDetected = pdTRUE;\r
184                 }\r
185 \r
186                 ( *puxLoopCounter )++;\r
187         }\r
188 \r
189         #if configUSE_PREEMPTION == 0\r
190                 taskYIELD();\r
191         #endif\r
192 \r
193         /* If the semaphore count is zero then we should not be able to 'take' \r
194         the semaphore. */\r
195         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
196         {\r
197                 xErrorDetected = pdTRUE;\r
198         }\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
203 {\r
204 unsigned portBASE_TYPE ux;\r
205 \r
206         /* If the semaphore count is zero then we should not be able to 'take' \r
207         the semaphore. */\r
208         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
209         {\r
210                 xErrorDetected = pdTRUE;\r
211         }\r
212 \r
213         /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */\r
214         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
215         {\r
216                 if( xSemaphoreGive( xSemaphore ) != pdPASS )\r
217                 {\r
218                         /* We expected to be able to take the semaphore. */\r
219                         xErrorDetected = pdTRUE;\r
220                 }\r
221 \r
222                 ( *puxLoopCounter )++;\r
223         }\r
224 \r
225         #if configUSE_PREEMPTION == 0\r
226                 taskYIELD();\r
227         #endif\r
228 \r
229         /* If the semaphore count is at its maximum then we should not be able to\r
230         'give' the semaphore. */\r
231         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
232         {\r
233                 xErrorDetected = pdTRUE;\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvCountingSemaphoreTask( void *pvParameters )\r
239 {\r
240 xCountSemStruct *pxParameter;\r
241 \r
242         #ifdef USE_STDIO\r
243         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
244         \r
245                 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";\r
246 \r
247                 /* Queue a message for printing to say the task has started. */\r
248                 vPrintDisplayMessage( &pcTaskStartMsg );\r
249         #endif\r
250 \r
251         /* The semaphore to be used was passed as the parameter. */\r
252         pxParameter = ( xCountSemStruct * ) pvParameters;\r
253 \r
254         /* Did we expect to find the semaphore already at its max count value, or\r
255         at zero? */\r
256         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )\r
257         {\r
258                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
259         }\r
260 \r
261         /* Now we expect the semaphore count to be 0, so this time there is an\r
262         error if we can take the semaphore. */\r
263         if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )\r
264         {\r
265                 xErrorDetected = pdTRUE;\r
266         }\r
267 \r
268         for( ;; )\r
269         {\r
270                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
271                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
272         }\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )\r
277 {\r
278 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;\r
279 portBASE_TYPE xReturn = pdPASS;\r
280 \r
281         /* Return fail if any 'give' or 'take' did not result in the expected\r
282         behaviour. */\r
283         if( xErrorDetected != pdFALSE )\r
284         {\r
285                 xReturn = pdFAIL;\r
286         }\r
287 \r
288         /* Return fail if either task is not still incrementing its loop counter. */\r
289         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )\r
290         {\r
291                 xReturn = pdFAIL;\r
292         }\r
293         else\r
294         {\r
295                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;\r
296         }\r
297 \r
298         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )\r
299         {\r
300                 xReturn = pdFAIL;\r
301         }\r
302         else\r
303         {\r
304                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;\r
305         }\r
306 \r
307         return xReturn;\r
308 }\r
309 \r
310 \r