]> begriffs open source - freertos/blob - Demo/Common/Minimal/countsem.c
Prepare for V5.3.0 release.
[freertos] / Demo / Common / Minimal / countsem.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* \r
54  * Simple demonstration of the usage of counting semaphore.\r
55  */\r
56 \r
57 /* Scheduler include files. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "semphr.h"\r
61 \r
62 /* Demo program include files. */\r
63 #include "countsem.h"\r
64 \r
65 /* The maximum count value that the semaphore used for the demo can hold. */\r
66 #define countMAX_COUNT_VALUE    ( 200 )\r
67 \r
68 /* Constants used to indicate whether or not the semaphore should have been\r
69 created with its maximum count value, or its minimum count value.  These \r
70 numbers are used to ensure that the pointers passed in as the task parameters\r
71 are valid. */\r
72 #define countSTART_AT_MAX_COUNT ( 0xaa )\r
73 #define countSTART_AT_ZERO              ( 0x55 )\r
74 \r
75 /* Two tasks are created for the test.  One uses a semaphore created with its\r
76 count value set to the maximum, and one with the count value set to zero. */\r
77 #define countNUM_TEST_TASKS             ( 2 )\r
78 #define countDONT_BLOCK                 ( 0 )\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
83 detected in any of the tasks. */\r
84 static volatile portBASE_TYPE xErrorDetected = pdFALSE;\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * The demo task.  This simply counts the semaphore up to its maximum value,\r
90  * the counts it back down again.  The result of each semaphore 'give' and\r
91  * 'take' is inspected, with an error being flagged if it is found not to be\r
92  * the expected result.\r
93  */\r
94 static void prvCountingSemaphoreTask( void *pvParameters );\r
95 \r
96 /*\r
97  * Utility function to increment the semaphore count value up from zero to\r
98  * countMAX_COUNT_VALUE.\r
99  */\r
100 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
101 \r
102 /*\r
103  * Utility function to decrement the semaphore count value up from \r
104  * countMAX_COUNT_VALUE to zero.\r
105  */\r
106 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* The structure that is passed into the task as the task parameter. */\r
111 typedef struct COUNT_SEM_STRUCT\r
112 {\r
113         /* The semaphore to be used for the demo. */\r
114         xSemaphoreHandle xSemaphore;\r
115 \r
116         /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with\r
117         its count value set to its max count value, or countSTART_AT_ZERO if it\r
118         should have been created with its count value set to 0. */\r
119         unsigned portBASE_TYPE uxExpectedStartCount;    \r
120 \r
121         /* Incremented on each cycle of the demo task.  Used to detect a stalled\r
122         task. */\r
123         unsigned portBASE_TYPE uxLoopCounter;                   \r
124 } xCountSemStruct;\r
125 \r
126 /* Two structures are defined, one is passed to each test task. */\r
127 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 void vStartCountingSemaphoreTasks( void )\r
132 {\r
133         /* Create the semaphores that we are going to use for the test/demo.  The\r
134         first should be created such that it starts at its maximum count value,\r
135         the second should be created such that it starts with a count value of zero. */\r
136         xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );\r
137         xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;\r
138         xParameters[ 0 ].uxLoopCounter = 0;\r
139 \r
140         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );\r
141         xParameters[ 1 ].uxExpectedStartCount = 0;\r
142         xParameters[ 1 ].uxLoopCounter = 0;\r
143 \r
144         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
145         in use.  The registry is provided as a means for kernel aware \r
146         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
147         is not being used.  The call to vQueueAddToRegistry() will be removed\r
148         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
149         defined to be less than 1. */\r
150         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );\r
151         vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );\r
152 \r
153 \r
154         /* Were the semaphores created? */\r
155         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )\r
156         {\r
157                 /* Create the demo tasks, passing in the semaphore to use as the parameter. */\r
158                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );\r
159                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );              \r
160         }\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
165 {\r
166 unsigned portBASE_TYPE ux;\r
167 \r
168         /* If the semaphore count is at its maximum then we should not be able to\r
169         'give' the semaphore. */\r
170         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
171         {\r
172                 xErrorDetected = pdTRUE;\r
173         }\r
174 \r
175         /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */\r
176         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
177         {\r
178                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )\r
179                 {\r
180                         /* We expected to be able to take the semaphore. */\r
181                         xErrorDetected = pdTRUE;\r
182                 }\r
183 \r
184                 ( *puxLoopCounter )++;\r
185         }\r
186 \r
187         #if configUSE_PREEMPTION == 0\r
188                 taskYIELD();\r
189         #endif\r
190 \r
191         /* If the semaphore count is zero then we should not be able to 'take' \r
192         the semaphore. */\r
193         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
194         {\r
195                 xErrorDetected = pdTRUE;\r
196         }\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
201 {\r
202 unsigned portBASE_TYPE ux;\r
203 \r
204         /* If the semaphore count is zero then we should not be able to 'take' \r
205         the semaphore. */\r
206         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
207         {\r
208                 xErrorDetected = pdTRUE;\r
209         }\r
210 \r
211         /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */\r
212         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
213         {\r
214                 if( xSemaphoreGive( xSemaphore ) != pdPASS )\r
215                 {\r
216                         /* We expected to be able to take the semaphore. */\r
217                         xErrorDetected = pdTRUE;\r
218                 }\r
219 \r
220                 ( *puxLoopCounter )++;\r
221         }\r
222 \r
223         #if configUSE_PREEMPTION == 0\r
224                 taskYIELD();\r
225         #endif\r
226 \r
227         /* If the semaphore count is at its maximum then we should not be able to\r
228         'give' the semaphore. */\r
229         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
230         {\r
231                 xErrorDetected = pdTRUE;\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 static void prvCountingSemaphoreTask( void *pvParameters )\r
237 {\r
238 xCountSemStruct *pxParameter;\r
239 \r
240         #ifdef USE_STDIO\r
241         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
242         \r
243                 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";\r
244 \r
245                 /* Queue a message for printing to say the task has started. */\r
246                 vPrintDisplayMessage( &pcTaskStartMsg );\r
247         #endif\r
248 \r
249         /* The semaphore to be used was passed as the parameter. */\r
250         pxParameter = ( xCountSemStruct * ) pvParameters;\r
251 \r
252         /* Did we expect to find the semaphore already at its max count value, or\r
253         at zero? */\r
254         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )\r
255         {\r
256                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
257         }\r
258 \r
259         /* Now we expect the semaphore count to be 0, so this time there is an\r
260         error if we can take the semaphore. */\r
261         if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )\r
262         {\r
263                 xErrorDetected = pdTRUE;\r
264         }\r
265 \r
266         for( ;; )\r
267         {\r
268                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
269                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
270         }\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )\r
275 {\r
276 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;\r
277 portBASE_TYPE xReturn = pdPASS;\r
278 \r
279         /* Return fail if any 'give' or 'take' did not result in the expected\r
280         behaviour. */\r
281         if( xErrorDetected != pdFALSE )\r
282         {\r
283                 xReturn = pdFAIL;\r
284         }\r
285 \r
286         /* Return fail if either task is not still incrementing its loop counter. */\r
287         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )\r
288         {\r
289                 xReturn = pdFAIL;\r
290         }\r
291         else\r
292         {\r
293                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;\r
294         }\r
295 \r
296         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )\r
297         {\r
298                 xReturn = pdFAIL;\r
299         }\r
300         else\r
301         {\r
302                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;\r
303         }\r
304 \r
305         return xReturn;\r
306 }\r
307 \r
308 \r