]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/countsem.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / Common / Minimal / countsem.c
1 /*\r
2     FreeRTOS V7.4.0 - 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 itcan 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 /* \r
77  * Simple demonstration of the usage of counting semaphore.\r
78  */\r
79 \r
80 /* Scheduler include files. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "semphr.h"\r
84 \r
85 /* Demo program include files. */\r
86 #include "countsem.h"\r
87 \r
88 /* The maximum count value that the semaphore used for the demo can hold. */\r
89 #define countMAX_COUNT_VALUE    ( 200 )\r
90 \r
91 /* Constants used to indicate whether or not the semaphore should have been\r
92 created with its maximum count value, or its minimum count value.  These \r
93 numbers are used to ensure that the pointers passed in as the task parameters\r
94 are valid. */\r
95 #define countSTART_AT_MAX_COUNT ( 0xaa )\r
96 #define countSTART_AT_ZERO              ( 0x55 )\r
97 \r
98 /* Two tasks are created for the test.  One uses a semaphore created with its\r
99 count value set to the maximum, and one with the count value set to zero. */\r
100 #define countNUM_TEST_TASKS             ( 2 )\r
101 #define countDONT_BLOCK                 ( 0 )\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
106 detected in any of the tasks. */\r
107 static volatile portBASE_TYPE xErrorDetected = pdFALSE;\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /*\r
112  * The demo task.  This simply counts the semaphore up to its maximum value,\r
113  * the counts it back down again.  The result of each semaphore 'give' and\r
114  * 'take' is inspected, with an error being flagged if it is found not to be\r
115  * the expected result.\r
116  */\r
117 static void prvCountingSemaphoreTask( void *pvParameters );\r
118 \r
119 /*\r
120  * Utility function to increment the semaphore count value up from zero to\r
121  * countMAX_COUNT_VALUE.\r
122  */\r
123 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
124 \r
125 /*\r
126  * Utility function to decrement the semaphore count value up from \r
127  * countMAX_COUNT_VALUE to zero.\r
128  */\r
129 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* The structure that is passed into the task as the task parameter. */\r
134 typedef struct COUNT_SEM_STRUCT\r
135 {\r
136         /* The semaphore to be used for the demo. */\r
137         xSemaphoreHandle xSemaphore;\r
138 \r
139         /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with\r
140         its count value set to its max count value, or countSTART_AT_ZERO if it\r
141         should have been created with its count value set to 0. */\r
142         unsigned portBASE_TYPE uxExpectedStartCount;    \r
143 \r
144         /* Incremented on each cycle of the demo task.  Used to detect a stalled\r
145         task. */\r
146         unsigned portBASE_TYPE uxLoopCounter;                   \r
147 } xCountSemStruct;\r
148 \r
149 /* Two structures are defined, one is passed to each test task. */\r
150 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 void vStartCountingSemaphoreTasks( void )\r
155 {\r
156         /* Create the semaphores that we are going to use for the test/demo.  The\r
157         first should be created such that it starts at its maximum count value,\r
158         the second should be created such that it starts with a count value of zero. */\r
159         xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );\r
160         xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;\r
161         xParameters[ 0 ].uxLoopCounter = 0;\r
162 \r
163         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );\r
164         xParameters[ 1 ].uxExpectedStartCount = 0;\r
165         xParameters[ 1 ].uxLoopCounter = 0;\r
166 \r
167         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
168         in use.  The registry is provided as a means for kernel aware \r
169         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
170         is not being used.  The call to vQueueAddToRegistry() will be removed\r
171         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
172         defined to be less than 1. */\r
173         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );\r
174         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );\r
175 \r
176 \r
177         /* Were the semaphores created? */\r
178         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )\r
179         {\r
180                 /* Create the demo tasks, passing in the semaphore to use as the parameter. */\r
181                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );\r
182                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );              \r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
188 {\r
189 unsigned portBASE_TYPE ux;\r
190 \r
191         /* If the semaphore count is at its maximum then we should not be able to\r
192         'give' the semaphore. */\r
193         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
194         {\r
195                 xErrorDetected = pdTRUE;\r
196         }\r
197 \r
198         /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */\r
199         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
200         {\r
201                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )\r
202                 {\r
203                         /* We expected to be able to take the semaphore. */\r
204                         xErrorDetected = pdTRUE;\r
205                 }\r
206 \r
207                 ( *puxLoopCounter )++;\r
208         }\r
209 \r
210         #if configUSE_PREEMPTION == 0\r
211                 taskYIELD();\r
212         #endif\r
213 \r
214         /* If the semaphore count is zero then we should not be able to 'take' \r
215         the semaphore. */\r
216         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
217         {\r
218                 xErrorDetected = pdTRUE;\r
219         }\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
224 {\r
225 unsigned portBASE_TYPE ux;\r
226 \r
227         /* If the semaphore count is zero then we should not be able to 'take' \r
228         the semaphore. */\r
229         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
230         {\r
231                 xErrorDetected = pdTRUE;\r
232         }\r
233 \r
234         /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */\r
235         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
236         {\r
237                 if( xSemaphoreGive( xSemaphore ) != pdPASS )\r
238                 {\r
239                         /* We expected to be able to take the semaphore. */\r
240                         xErrorDetected = pdTRUE;\r
241                 }\r
242 \r
243                 ( *puxLoopCounter )++;\r
244         }\r
245 \r
246         #if configUSE_PREEMPTION == 0\r
247                 taskYIELD();\r
248         #endif\r
249 \r
250         /* If the semaphore count is at its maximum then we should not be able to\r
251         'give' the semaphore. */\r
252         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
253         {\r
254                 xErrorDetected = pdTRUE;\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void prvCountingSemaphoreTask( void *pvParameters )\r
260 {\r
261 xCountSemStruct *pxParameter;\r
262 \r
263         #ifdef USE_STDIO\r
264         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
265         \r
266                 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";\r
267 \r
268                 /* Queue a message for printing to say the task has started. */\r
269                 vPrintDisplayMessage( &pcTaskStartMsg );\r
270         #endif\r
271 \r
272         /* The semaphore to be used was passed as the parameter. */\r
273         pxParameter = ( xCountSemStruct * ) pvParameters;\r
274 \r
275         /* Did we expect to find the semaphore already at its max count value, or\r
276         at zero? */\r
277         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )\r
278         {\r
279                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
280         }\r
281 \r
282         /* Now we expect the semaphore count to be 0, so this time there is an\r
283         error if we can take the semaphore. */\r
284         if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )\r
285         {\r
286                 xErrorDetected = pdTRUE;\r
287         }\r
288 \r
289         for( ;; )\r
290         {\r
291                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
292                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )\r
298 {\r
299 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;\r
300 portBASE_TYPE xReturn = pdPASS;\r
301 \r
302         /* Return fail if any 'give' or 'take' did not result in the expected\r
303         behaviour. */\r
304         if( xErrorDetected != pdFALSE )\r
305         {\r
306                 xReturn = pdFAIL;\r
307         }\r
308 \r
309         /* Return fail if either task is not still incrementing its loop counter. */\r
310         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )\r
311         {\r
312                 xReturn = pdFAIL;\r
313         }\r
314         else\r
315         {\r
316                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;\r
317         }\r
318 \r
319         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )\r
320         {\r
321                 xReturn = pdFAIL;\r
322         }\r
323         else\r
324         {\r
325                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;\r
326         }\r
327 \r
328         return xReturn;\r
329 }\r
330 \r
331 \r