]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/countsem.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / Minimal / countsem.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 \r
68 /* \r
69  * Simple demonstration of the usage of counting semaphore.\r
70  */\r
71 \r
72 /* Scheduler include files. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 #include "semphr.h"\r
76 \r
77 /* Demo program include files. */\r
78 #include "countsem.h"\r
79 \r
80 /* The maximum count value that the semaphore used for the demo can hold. */\r
81 #define countMAX_COUNT_VALUE    ( 200 )\r
82 \r
83 /* Constants used to indicate whether or not the semaphore should have been\r
84 created with its maximum count value, or its minimum count value.  These \r
85 numbers are used to ensure that the pointers passed in as the task parameters\r
86 are valid. */\r
87 #define countSTART_AT_MAX_COUNT ( 0xaa )\r
88 #define countSTART_AT_ZERO              ( 0x55 )\r
89 \r
90 /* Two tasks are created for the test.  One uses a semaphore created with its\r
91 count value set to the maximum, and one with the count value set to zero. */\r
92 #define countNUM_TEST_TASKS             ( 2 )\r
93 #define countDONT_BLOCK                 ( 0 )\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
98 detected in any of the tasks. */\r
99 static volatile portBASE_TYPE xErrorDetected = pdFALSE;\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /*\r
104  * The demo task.  This simply counts the semaphore up to its maximum value,\r
105  * the counts it back down again.  The result of each semaphore 'give' and\r
106  * 'take' is inspected, with an error being flagged if it is found not to be\r
107  * the expected result.\r
108  */\r
109 static void prvCountingSemaphoreTask( void *pvParameters );\r
110 \r
111 /*\r
112  * Utility function to increment the semaphore count value up from zero to\r
113  * countMAX_COUNT_VALUE.\r
114  */\r
115 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
116 \r
117 /*\r
118  * Utility function to decrement the semaphore count value up from \r
119  * countMAX_COUNT_VALUE to zero.\r
120  */\r
121 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /* The structure that is passed into the task as the task parameter. */\r
126 typedef struct COUNT_SEM_STRUCT\r
127 {\r
128         /* The semaphore to be used for the demo. */\r
129         xSemaphoreHandle xSemaphore;\r
130 \r
131         /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with\r
132         its count value set to its max count value, or countSTART_AT_ZERO if it\r
133         should have been created with its count value set to 0. */\r
134         unsigned portBASE_TYPE uxExpectedStartCount;    \r
135 \r
136         /* Incremented on each cycle of the demo task.  Used to detect a stalled\r
137         task. */\r
138         unsigned portBASE_TYPE uxLoopCounter;                   \r
139 } xCountSemStruct;\r
140 \r
141 /* Two structures are defined, one is passed to each test task. */\r
142 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void vStartCountingSemaphoreTasks( void )\r
147 {\r
148         /* Create the semaphores that we are going to use for the test/demo.  The\r
149         first should be created such that it starts at its maximum count value,\r
150         the second should be created such that it starts with a count value of zero. */\r
151         xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );\r
152         xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;\r
153         xParameters[ 0 ].uxLoopCounter = 0;\r
154 \r
155         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );\r
156         xParameters[ 1 ].uxExpectedStartCount = 0;\r
157         xParameters[ 1 ].uxLoopCounter = 0;\r
158 \r
159         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
160         in use.  The registry is provided as a means for kernel aware \r
161         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
162         is not being used.  The call to vQueueAddToRegistry() will be removed\r
163         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
164         defined to be less than 1. */\r
165         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );\r
166         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );\r
167 \r
168 \r
169         /* Were the semaphores created? */\r
170         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )\r
171         {\r
172                 /* Create the demo tasks, passing in the semaphore to use as the parameter. */\r
173                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );\r
174                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );              \r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
180 {\r
181 unsigned portBASE_TYPE ux;\r
182 \r
183         /* If the semaphore count is at its maximum then we should not be able to\r
184         'give' the semaphore. */\r
185         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
186         {\r
187                 xErrorDetected = pdTRUE;\r
188         }\r
189 \r
190         /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */\r
191         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
192         {\r
193                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )\r
194                 {\r
195                         /* We expected to be able to take the semaphore. */\r
196                         xErrorDetected = pdTRUE;\r
197                 }\r
198 \r
199                 ( *puxLoopCounter )++;\r
200         }\r
201 \r
202         #if configUSE_PREEMPTION == 0\r
203                 taskYIELD();\r
204         #endif\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 /*-----------------------------------------------------------*/\r
214 \r
215 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
216 {\r
217 unsigned portBASE_TYPE ux;\r
218 \r
219         /* If the semaphore count is zero then we should not be able to 'take' \r
220         the semaphore. */\r
221         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
222         {\r
223                 xErrorDetected = pdTRUE;\r
224         }\r
225 \r
226         /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */\r
227         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
228         {\r
229                 if( xSemaphoreGive( xSemaphore ) != pdPASS )\r
230                 {\r
231                         /* We expected to be able to take the semaphore. */\r
232                         xErrorDetected = pdTRUE;\r
233                 }\r
234 \r
235                 ( *puxLoopCounter )++;\r
236         }\r
237 \r
238         #if configUSE_PREEMPTION == 0\r
239                 taskYIELD();\r
240         #endif\r
241 \r
242         /* If the semaphore count is at its maximum then we should not be able to\r
243         'give' the semaphore. */\r
244         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
245         {\r
246                 xErrorDetected = pdTRUE;\r
247         }\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvCountingSemaphoreTask( void *pvParameters )\r
252 {\r
253 xCountSemStruct *pxParameter;\r
254 \r
255         #ifdef USE_STDIO\r
256         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
257         \r
258                 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";\r
259 \r
260                 /* Queue a message for printing to say the task has started. */\r
261                 vPrintDisplayMessage( &pcTaskStartMsg );\r
262         #endif\r
263 \r
264         /* The semaphore to be used was passed as the parameter. */\r
265         pxParameter = ( xCountSemStruct * ) pvParameters;\r
266 \r
267         /* Did we expect to find the semaphore already at its max count value, or\r
268         at zero? */\r
269         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )\r
270         {\r
271                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
272         }\r
273 \r
274         /* Now we expect the semaphore count to be 0, so this time there is an\r
275         error if we can take the semaphore. */\r
276         if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )\r
277         {\r
278                 xErrorDetected = pdTRUE;\r
279         }\r
280 \r
281         for( ;; )\r
282         {\r
283                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
284                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
285         }\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )\r
290 {\r
291 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;\r
292 portBASE_TYPE xReturn = pdPASS;\r
293 \r
294         /* Return fail if any 'give' or 'take' did not result in the expected\r
295         behaviour. */\r
296         if( xErrorDetected != pdFALSE )\r
297         {\r
298                 xReturn = pdFAIL;\r
299         }\r
300 \r
301         /* Return fail if either task is not still incrementing its loop counter. */\r
302         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )\r
303         {\r
304                 xReturn = pdFAIL;\r
305         }\r
306         else\r
307         {\r
308                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;\r
309         }\r
310 \r
311         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )\r
312         {\r
313                 xReturn = pdFAIL;\r
314         }\r
315         else\r
316         {\r
317                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;\r
318         }\r
319 \r
320         return xReturn;\r
321 }\r
322 \r
323 \r