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