root/webserver/example/freeRTOSexample/EnergyMeters/Common/Minimal/countsem.c

Revision 14, 10.5 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

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