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

Subversion Repositories openrisc

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

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
 * Creates two sets of two tasks.  The tasks within a set share a variable, access
56
 * to which is guarded by a semaphore.
57
 *
58
 * Each task starts by attempting to obtain the semaphore.  On obtaining a
59
 * semaphore a task checks to ensure that the guarded variable has an expected
60
 * value.  It then clears the variable to zero before counting it back up to the
61
 * expected value in increments of 1.  After each increment the variable is checked
62
 * to ensure it contains the value to which it was just set. When the starting
63
 * value is again reached the task releases the semaphore giving the other task in
64
 * the set a chance to do exactly the same thing.  The starting value is high
65
 * enough to ensure that a tick is likely to occur during the incrementing loop.
66
 *
67
 * An error is flagged if at any time during the process a shared variable is
68
 * found to have a value other than that expected.  Such an occurrence would
69
 * suggest an error in the mutual exclusion mechanism by which access to the
70
 * variable is restricted.
71
 *
72
 * The first set of two tasks poll their semaphore.  The second set use blocking
73
 * calls.
74
 *
75
 */
76
 
77
 
78
#include <stdlib.h>
79
 
80
/* Scheduler include files. */
81
#include "FreeRTOS.h"
82
#include "task.h"
83
#include "semphr.h"
84
 
85
/* Demo app include files. */
86
#include "semtest.h"
87
 
88
/* The value to which the shared variables are counted. */
89
#define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned long ) 0xfff )
90
#define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned long ) 0xff  )
91
 
92
#define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE
93
 
94
#define semtstNUM_TASKS                         ( 4 )
95
 
96
#define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )
97
 
98
/* The task function as described at the top of the file. */
99
static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
100
 
101
/* Structure used to pass parameters to each task. */
102
typedef struct SEMAPHORE_PARAMETERS
103
{
104
        xSemaphoreHandle xSemaphore;
105
        volatile unsigned long *pulSharedVariable;
106
        portTickType xBlockTime;
107
} xSemaphoreParameters;
108
 
109
/* Variables used to check that all the tasks are still running without errors. */
110
static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
111
static volatile short sNextCheckVariable = 0;
112
 
113
/*-----------------------------------------------------------*/
114
 
115
void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
116
{
117
xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
118
const portTickType xBlockTime = ( portTickType ) 100;
119
 
120
        /* Create the structure used to pass parameters to the first two tasks. */
121
        pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
122
 
123
        if( pxFirstSemaphoreParameters != NULL )
124
        {
125
                /* Create the semaphore used by the first two tasks. */
126
                vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
127
 
128
                if( pxFirstSemaphoreParameters->xSemaphore != NULL )
129
                {
130
                        /* Create the variable which is to be shared by the first two tasks. */
131
                        pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
132
 
133
                        /* Initialise the share variable to the value the tasks expect. */
134
                        *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
135
 
136
                        /* The first two tasks do not block on semaphore calls. */
137
                        pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
138
 
139
                        /* Spawn the first two tasks.  As they poll they operate at the idle priority. */
140
                        xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
141
                        xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
142
                }
143
        }
144
 
145
        /* Do exactly the same to create the second set of tasks, only this time
146
        provide a block time for the semaphore calls. */
147
        pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
148
        if( pxSecondSemaphoreParameters != NULL )
149
        {
150
                vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
151
 
152
                if( pxSecondSemaphoreParameters->xSemaphore != NULL )
153
                {
154
                        pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
155
                        *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
156
                        pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
157
 
158
                        xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
159
                        xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
160
                }
161
        }
162
 
163
        /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
164
        in use.  The registry is provided as a means for kernel aware
165
        debuggers to locate semaphores and has no purpose if a kernel aware debugger
166
        is not being used.  The call to vQueueAddToRegistry() will be removed
167
        by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
168
        defined to be less than 1. */
169
        vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_1" );
170
        vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_2" );
171
}
172
/*-----------------------------------------------------------*/
173
 
174
static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
175
{
176
xSemaphoreParameters *pxParameters;
177
volatile unsigned long *pulSharedVariable, ulExpectedValue;
178
unsigned long ulCounter;
179
short sError = pdFALSE, sCheckVariableToUse;
180
 
181
        /* See which check variable to use.  sNextCheckVariable is not semaphore
182
        protected! */
183
        portENTER_CRITICAL();
184
                sCheckVariableToUse = sNextCheckVariable;
185
                sNextCheckVariable++;
186
        portEXIT_CRITICAL();
187
 
188
        /* A structure is passed in as the parameter.  This contains the shared
189
        variable being guarded. */
190
        pxParameters = ( xSemaphoreParameters * ) pvParameters;
191
        pulSharedVariable = pxParameters->pulSharedVariable;
192
 
193
        /* If we are blocking we use a much higher count to ensure loads of context
194
        switches occur during the count. */
195
        if( pxParameters->xBlockTime > ( portTickType ) 0 )
196
        {
197
                ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
198
        }
199
        else
200
        {
201
                ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
202
        }
203
 
204
        for( ;; )
205
        {
206
                /* Try to obtain the semaphore. */
207
                if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
208
                {
209
                        /* We have the semaphore and so expect any other tasks using the
210
                        shared variable to have left it in the state we expect to find
211
                        it. */
212
                        if( *pulSharedVariable != ulExpectedValue )
213
                        {
214
                                sError = pdTRUE;
215
                        }
216
 
217
                        /* Clear the variable, then count it back up to the expected value
218
                        before releasing the semaphore.  Would expect a context switch or
219
                        two during this time. */
220
                        for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
221
                        {
222
                                *pulSharedVariable = ulCounter;
223
                                if( *pulSharedVariable != ulCounter )
224
                                {
225
                                        sError = pdTRUE;
226
                                }
227
                        }
228
 
229
                        /* Release the semaphore, and if no errors have occurred increment the check
230
                        variable. */
231
                        if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
232
                        {
233
                                sError = pdTRUE;
234
                        }
235
 
236
                        if( sError == pdFALSE )
237
                        {
238
                                if( sCheckVariableToUse < semtstNUM_TASKS )
239
                                {
240
                                        ( sCheckVariables[ sCheckVariableToUse ] )++;
241
                                }
242
                        }
243
 
244
                        /* If we have a block time then we are running at a priority higher
245
                        than the idle priority.  This task takes a long time to complete
246
                        a cycle (deliberately so to test the guarding) so will be starving
247
                        out lower priority tasks.  Block for some time to allow give lower
248
                        priority tasks some processor time. */
249
                        vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
250
                }
251
                else
252
                {
253
                        if( pxParameters->xBlockTime == ( portTickType ) 0 )
254
                        {
255
                                /* We have not got the semaphore yet, so no point using the
256
                                processor.  We are not blocking when attempting to obtain the
257
                                semaphore. */
258
                                taskYIELD();
259
                        }
260
                }
261
        }
262
}
263
/*-----------------------------------------------------------*/
264
 
265
/* This is called to check that all the created tasks are still running. */
266
portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
267
{
268
static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
269
portBASE_TYPE xTask, xReturn = pdTRUE;
270
 
271
        for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
272
        {
273
                if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
274
                {
275
                        xReturn = pdFALSE;
276
                }
277
 
278
                sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
279
        }
280
 
281
        return xReturn;
282
}
283
 
284
 

powered by: WebSVN 2.1.0

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