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