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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [Full/] [BlockQ.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
 * 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
 * \page BlockQC blockQ.c
77
 * \ingroup DemoFiles
78
 * <HR>
79
 */
80
 
81
/*
82
Changes from V1.00:
83
 
84
        + Reversed the priority and block times of the second two demo tasks so
85
          they operate as per the description above.
86
 
87
Changes from V2.0.0
88
 
89
        + Delay periods are now specified using variables and constants of
90
          portTickType rather than unsigned long.
91
 
92
Changes from V4.0.2
93
 
94
        + The second set of tasks were created the wrong way around.  This has been
95
          corrected.
96
*/
97
 
98
 
99
#include <stdlib.h>
100
 
101
/* Scheduler include files. */
102
#include "FreeRTOS.h"
103
#include "task.h"
104
#include "queue.h"
105
 
106
/* Demo program include files. */
107
#include "BlockQ.h"
108
#include "print.h"
109
 
110
#define blckqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )
111
#define blckqNUM_TASK_SETS      ( 3 )
112
 
113
/* Structure used to pass parameters to the blocking queue tasks. */
114
typedef struct BLOCKING_QUEUE_PARAMETERS
115
{
116
        xQueueHandle xQueue;                                    /*< The queue to be used by the task. */
117
        portTickType xBlockTime;                        /*< The block time to use on queue reads/writes. */
118
        volatile short *psCheckVariable;        /*< Incremented on each successful cycle to check the task is still running. */
119
} xBlockingQueueParameters;
120
 
121
/* Task function that creates an incrementing number and posts it on a queue. */
122
static void vBlockingQueueProducer( void *pvParameters );
123
 
124
/* Task function that removes the incrementing number from a queue and checks that
125
it is the expected number. */
126
static void vBlockingQueueConsumer( void *pvParameters );
127
 
128
/* Variables which are incremented each time an item is removed from a queue, and
129
found to be the expected value.
130
These are used to check that the tasks are still running. */
131
static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
132
 
133
/* Variable which are incremented each time an item is posted on a queue.   These
134
are used to check that the tasks are still running. */
135
static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
136
 
137
/*-----------------------------------------------------------*/
138
 
139
void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )
140
{
141
xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;
142
xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;
143
xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;
144
const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;
145
const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;
146
const portTickType xDontBlock = ( portTickType ) 0;
147
 
148
        /* Create the first two tasks as described at the top of the file. */
149
 
150
        /* First create the structure used to pass parameters to the consumer tasks. */
151
        pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
152
 
153
        /* Create the queue used by the first two tasks to pass the incrementing number.
154
        Pass a pointer to the queue in the parameter structure. */
155
        pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
156
 
157
        /* The consumer is created first so gets a block time as described above. */
158
        pxQueueParameters1->xBlockTime = xBlockTime;
159
 
160
        /* Pass in the variable that this task is going to increment so we can check it
161
        is still running. */
162
        pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );
163
 
164
        /* Create the structure used to pass parameters to the producer task. */
165
        pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
166
 
167
        /* Pass the queue to this task also, using the parameter structure. */
168
        pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
169
 
170
        /* The producer is not going to block - as soon as it posts the consumer will
171
        wake and remove the item so the producer should always have room to post. */
172
        pxQueueParameters2->xBlockTime = xDontBlock;
173
 
174
        /* Pass in the variable that this task is going to increment so we can check
175
        it is still running. */
176
        pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );
177
 
178
 
179
        /* Note the producer has a lower priority than the consumer when the tasks are
180
        spawned. */
181
        xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
182
        xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
183
 
184
 
185
 
186
        /* Create the second two tasks as described at the top of the file.   This uses
187
        the same mechanism but reverses the task priorities. */
188
 
189
        pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
190
        pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
191
        pxQueueParameters3->xBlockTime = xDontBlock;
192
        pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );
193
 
194
        pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
195
        pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
196
        pxQueueParameters4->xBlockTime = xBlockTime;
197
        pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
198
 
199
        xTaskCreate( vBlockingQueueProducer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
200
        xTaskCreate( vBlockingQueueConsumer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
201
 
202
 
203
 
204
        /* Create the last two tasks as described above.  The mechanism is again just
205
        the same.  This time both parameter structures are given a block time. */
206
        pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
207
        pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
208
        pxQueueParameters5->xBlockTime = xBlockTime;
209
        pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );
210
 
211
        pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
212
        pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
213
        pxQueueParameters6->xBlockTime = xBlockTime;
214
        pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
215
 
216
        xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );
217
        xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );
218
}
219
/*-----------------------------------------------------------*/
220
 
221
static void vBlockingQueueProducer( void *pvParameters )
222
{
223
unsigned short usValue = 0;
224
xBlockingQueueParameters *pxQueueParameters;
225
const char * const pcTaskStartMsg = "Blocking queue producer started.\r\n";
226
const char * const pcTaskErrorMsg = "Could not post on blocking queue\r\n";
227
short sErrorEverOccurred = pdFALSE;
228
 
229
        pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
230
 
231
        /* Queue a message for printing to say the task has started. */
232
        vPrintDisplayMessage( &pcTaskStartMsg );
233
 
234
        for( ;; )
235
        {
236
                if( xQueueSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
237
                {
238
                        vPrintDisplayMessage( &pcTaskErrorMsg );
239
                        sErrorEverOccurred = pdTRUE;
240
                }
241
                else
242
                {
243
                        /* We have successfully posted a message, so increment the variable
244
                        used to check we are still running. */
245
                        if( sErrorEverOccurred == pdFALSE )
246
                        {
247
                                ( *pxQueueParameters->psCheckVariable )++;
248
                        }
249
 
250
                        /* Increment the variable we are going to post next time round.  The
251
                        consumer will expect the numbers to     follow in numerical order. */
252
                        ++usValue;
253
                }
254
        }
255
}
256
/*-----------------------------------------------------------*/
257
 
258
static void vBlockingQueueConsumer( void *pvParameters )
259
{
260
unsigned short usData, usExpectedValue = 0;
261
xBlockingQueueParameters *pxQueueParameters;
262
const char * const pcTaskStartMsg = "Blocking queue consumer started.\r\n";
263
const char * const pcTaskErrorMsg = "Incorrect value received on blocking queue.\r\n";
264
short sErrorEverOccurred = pdFALSE;
265
 
266
        /* Queue a message for printing to say the task has started. */
267
        vPrintDisplayMessage( &pcTaskStartMsg );
268
 
269
        pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
270
 
271
        for( ;; )
272
        {
273
                if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
274
                {
275
                        if( usData != usExpectedValue )
276
                        {
277
                                vPrintDisplayMessage( &pcTaskErrorMsg );
278
 
279
                                /* Catch-up. */
280
                                usExpectedValue = usData;
281
 
282
                                sErrorEverOccurred = pdTRUE;
283
                        }
284
                        else
285
                        {
286
                                /* We have successfully received a message, so increment the
287
                                variable used to check we are still running. */
288
                                if( sErrorEverOccurred == pdFALSE )
289
                                {
290
                                        ( *pxQueueParameters->psCheckVariable )++;
291
                                }
292
 
293
                                /* Increment the value we expect to remove from the queue next time
294
                                round. */
295
                                ++usExpectedValue;
296
                        }
297
                }
298
        }
299
}
300
/*-----------------------------------------------------------*/
301
 
302
/* This is called to check that all the created tasks are still running. */
303
portBASE_TYPE xAreBlockingQueuesStillRunning( void )
304
{
305
static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
306
static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
307
portBASE_TYPE xReturn = pdPASS, xTasks;
308
 
309
        /* Not too worried about mutual exclusion on these variables as they are 16
310
        bits and we are only reading them. We also only care to see if they have
311
        changed or not.
312
 
313
        Loop through each check variable and return pdFALSE if any are found not
314
        to have changed since the last call. */
315
 
316
        for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
317
        {
318
                if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )
319
                {
320
                        xReturn = pdFALSE;
321
                }
322
                sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
323
 
324
 
325
                if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )
326
                {
327
                        xReturn = pdFALSE;
328
                }
329
                sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
330
        }
331
 
332
        return xReturn;
333
}
334
 

powered by: WebSVN 2.1.0

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