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

Subversion Repositories openrisc

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

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 six tasks that operate on three queues as follows:
56
 *
57
 * The first two tasks send and receive an incrementing number to/from a queue.
58
 * One task acts as a producer and the other as the consumer.  The consumer is a
59
 * higher priority than the producer and is set to block on queue reads.  The queue
60
 * only has space for one item - as soon as the producer posts a message on the
61
 * queue the consumer will unblock, pre-empt the producer, and remove the item.
62
 *
63
 * The second two tasks work the other way around.  Again the queue used only has
64
 * enough space for one item.  This time the consumer has a lower priority than the
65
 * producer.  The producer will try to post on the queue blocking when the queue is
66
 * full.  When the consumer wakes it will remove the item from the queue, causing
67
 * the producer to unblock, pre-empt the consumer, and immediately re-fill the
68
 * queue.
69
 *
70
 * The last two tasks use the same queue producer and consumer functions.  This time the queue has
71
 * enough space for lots of items and the tasks operate at the same priority.  The
72
 * producer will execute, placing items into the queue.  The consumer will start
73
 * executing when either the queue becomes full (causing the producer to block) or
74
 * a context switch occurs (tasks of the same priority will time slice).
75
 *
76
 */
77
 
78
/*
79
 
80
Changes from V4.1.1
81
 
82
        + The second set of tasks were created the wrong way around.  This has been
83
          corrected.
84
*/
85
 
86
 
87
#include <stdlib.h>
88
 
89
/* Scheduler include files. */
90
#include "FreeRTOS.h"
91
#include "task.h"
92
#include "queue.h"
93
 
94
/* Demo program include files. */
95
#include "BlockQ.h"
96
 
97
#define blckqSTACK_SIZE         configMINIMAL_STACK_SIZE
98
#define blckqNUM_TASK_SETS      ( 3 )
99
 
100
/* Structure used to pass parameters to the blocking queue tasks. */
101
typedef struct BLOCKING_QUEUE_PARAMETERS
102
{
103
        xQueueHandle xQueue;                                    /*< The queue to be used by the task. */
104
        portTickType xBlockTime;                                /*< The block time to use on queue reads/writes. */
105
        volatile short *psCheckVariable;        /*< Incremented on each successful cycle to check the task is still running. */
106
} xBlockingQueueParameters;
107
 
108
/* Task function that creates an incrementing number and posts it on a queue. */
109
static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );
110
 
111
/* Task function that removes the incrementing number from a queue and checks that
112
it is the expected number. */
113
static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );
114
 
115
/* Variables which are incremented each time an item is removed from a queue, and
116
found to be the expected value.
117
These are used to check that the tasks are still running. */
118
static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
119
 
120
/* Variable which are incremented each time an item is posted on a queue.   These
121
are used to check that the tasks are still running. */
122
static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
123
 
124
/*-----------------------------------------------------------*/
125
 
126
void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )
127
{
128
xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;
129
xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;
130
xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;
131
const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;
132
const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;
133
const portTickType xDontBlock = ( portTickType ) 0;
134
 
135
        /* Create the first two tasks as described at the top of the file. */
136
 
137
        /* First create the structure used to pass parameters to the consumer tasks. */
138
        pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
139
 
140
        /* Create the queue used by the first two tasks to pass the incrementing number.
141
        Pass a pointer to the queue in the parameter structure. */
142
        pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
143
 
144
        /* The consumer is created first so gets a block time as described above. */
145
        pxQueueParameters1->xBlockTime = xBlockTime;
146
 
147
        /* Pass in the variable that this task is going to increment so we can check it
148
        is still running. */
149
        pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );
150
 
151
        /* Create the structure used to pass parameters to the producer task. */
152
        pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
153
 
154
        /* Pass the queue to this task also, using the parameter structure. */
155
        pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
156
 
157
        /* The producer is not going to block - as soon as it posts the consumer will
158
        wake and remove the item so the producer should always have room to post. */
159
        pxQueueParameters2->xBlockTime = xDontBlock;
160
 
161
        /* Pass in the variable that this task is going to increment so we can check
162
        it is still running. */
163
        pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );
164
 
165
 
166
        /* Note the producer has a lower priority than the consumer when the tasks are
167
        spawned. */
168
        xTaskCreate( vBlockingQueueConsumer, ( signed char * ) "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
169
        xTaskCreate( vBlockingQueueProducer, ( signed char * ) "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
170
 
171
 
172
 
173
        /* Create the second two tasks as described at the top of the file.   This uses
174
        the same mechanism but reverses the task priorities. */
175
 
176
        pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
177
        pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
178
        pxQueueParameters3->xBlockTime = xDontBlock;
179
        pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );
180
 
181
        pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
182
        pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
183
        pxQueueParameters4->xBlockTime = xBlockTime;
184
        pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
185
 
186
        xTaskCreate( vBlockingQueueConsumer, ( signed char * ) "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
187
        xTaskCreate( vBlockingQueueProducer, ( signed char * ) "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
188
 
189
 
190
 
191
        /* Create the last two tasks as described above.  The mechanism is again just
192
        the same.  This time both parameter structures are given a block time. */
193
        pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
194
        pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
195
        pxQueueParameters5->xBlockTime = xBlockTime;
196
        pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );
197
 
198
        pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
199
        pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
200
        pxQueueParameters6->xBlockTime = xBlockTime;
201
        pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
202
 
203
        xTaskCreate( vBlockingQueueProducer, ( signed char * ) "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );
204
        xTaskCreate( vBlockingQueueConsumer, ( signed char * ) "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );
205
}
206
/*-----------------------------------------------------------*/
207
 
208
static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )
209
{
210
unsigned short usValue = 0;
211
xBlockingQueueParameters *pxQueueParameters;
212
short sErrorEverOccurred = pdFALSE;
213
 
214
        pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
215
 
216
        for( ;; )
217
        {
218
                if( xQueueSend( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
219
                {
220
                        sErrorEverOccurred = pdTRUE;
221
                }
222
                else
223
                {
224
                        /* We have successfully posted a message, so increment the variable
225
                        used to check we are still running. */
226
                        if( sErrorEverOccurred == pdFALSE )
227
                        {
228
                                ( *pxQueueParameters->psCheckVariable )++;
229
                        }
230
 
231
                        /* Increment the variable we are going to post next time round.  The
232
                        consumer will expect the numbers to     follow in numerical order. */
233
                        ++usValue;
234
                }
235
        }
236
}
237
/*-----------------------------------------------------------*/
238
 
239
static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )
240
{
241
unsigned short usData, usExpectedValue = 0;
242
xBlockingQueueParameters *pxQueueParameters;
243
short sErrorEverOccurred = pdFALSE;
244
 
245
        pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
246
 
247
        for( ;; )
248
        {
249
                if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
250
                {
251
                        if( usData != usExpectedValue )
252
                        {
253
                                /* Catch-up. */
254
                                usExpectedValue = usData;
255
 
256
                                sErrorEverOccurred = pdTRUE;
257
                        }
258
                        else
259
                        {
260
                                /* We have successfully received a message, so increment the
261
                                variable used to check we are still running. */
262
                                if( sErrorEverOccurred == pdFALSE )
263
                                {
264
                                        ( *pxQueueParameters->psCheckVariable )++;
265
                                }
266
 
267
                                /* Increment the value we expect to remove from the queue next time
268
                                round. */
269
                                ++usExpectedValue;
270
                        }
271
                }
272
        }
273
}
274
/*-----------------------------------------------------------*/
275
 
276
/* This is called to check that all the created tasks are still running. */
277
portBASE_TYPE xAreBlockingQueuesStillRunning( void )
278
{
279
static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
280
static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
281
portBASE_TYPE xReturn = pdPASS, xTasks;
282
 
283
        /* Not too worried about mutual exclusion on these variables as they are 16
284
        bits and we are only reading them. We also only care to see if they have
285
        changed or not.
286
 
287
        Loop through each check variable to and return pdFALSE if any are found not
288
        to have changed since the last call. */
289
 
290
        for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
291
        {
292
                if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )
293
                {
294
                        xReturn = pdFALSE;
295
                }
296
                sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
297
 
298
 
299
                if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )
300
                {
301
                        xReturn = pdFALSE;
302
                }
303
                sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
304
        }
305
 
306
        return xReturn;
307
}
308
 

powered by: WebSVN 2.1.0

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