OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [Minimal/] [countsem.c] - Blame information for rev 609

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.