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