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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [Minimal/] [AltBlock.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
 * This is a version of BlockTim.c that uses the light weight API.
56
 *
57
 * This file contains some test scenarios that ensure tasks do not exit queue
58
 * send or receive functions prematurely.  A description of the tests is
59
 * included within the code.
60
 */
61
 
62
/* Kernel includes. */
63
#include "FreeRTOS.h"
64
#include "task.h"
65
#include "queue.h"
66
 
67
/* Demo includes. */
68
#include "AltBlock.h"
69
 
70
/* Task priorities. */
71
#define bktPRIMARY_PRIORITY                     ( 3 )
72
#define bktSECONDARY_PRIORITY           ( 2 )
73
 
74
/* Task behaviour. */
75
#define bktQUEUE_LENGTH                         ( 5 )
76
#define bktSHORT_WAIT                           ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
77
#define bktPRIMARY_BLOCK_TIME           ( 10 )
78
#define bktALLOWABLE_MARGIN                     ( 12 )
79
#define bktTIME_TO_BLOCK                        ( 175 )
80
#define bktDONT_BLOCK                           ( ( portTickType ) 0 )
81
#define bktRUN_INDICATOR                        ( ( unsigned portBASE_TYPE ) 0x55 )
82
 
83
/* The queue on which the tasks block. */
84
static xQueueHandle xTestQueue;
85
 
86
/* Handle to the secondary task is required by the primary task for calls
87
to vTaskSuspend/Resume(). */
88
static xTaskHandle xSecondary;
89
 
90
/* Used to ensure that tasks are still executing without error. */
91
static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
92
static portBASE_TYPE xErrorOccurred = pdFALSE;
93
 
94
/* Provides a simple mechanism for the primary task to know when the
95
secondary task has executed. */
96
static volatile unsigned portBASE_TYPE xRunIndicator;
97
 
98
/* The two test tasks.  Their behaviour is commented within the files. */
99
static void vPrimaryBlockTimeTestTask( void *pvParameters );
100
static void vSecondaryBlockTimeTestTask( void *pvParameters );
101
 
102
/*-----------------------------------------------------------*/
103
 
104
void vCreateAltBlockTimeTasks( void )
105
{
106
        /* Create the queue on which the two tasks block. */
107
    xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
108
 
109
        /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
110
        in use.  The queue registry is provided as a means for kernel aware
111
        debuggers to locate queues and has no purpose if a kernel aware debugger
112
        is not being used.  The call to vQueueAddToRegistry() will be removed
113
        by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
114
        defined to be less than 1. */
115
        vQueueAddToRegistry( xTestQueue, ( signed portCHAR * ) "AltBlockQueue" );
116
 
117
 
118
        /* Create the two test tasks. */
119
        xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"FBTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
120
        xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"FBTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
121
}
122
/*-----------------------------------------------------------*/
123
 
124
static void vPrimaryBlockTimeTestTask( void *pvParameters )
125
{
126
portBASE_TYPE xItem, xData;
127
portTickType xTimeWhenBlocking;
128
portTickType xTimeToBlock, xBlockedTime;
129
 
130
        #ifdef USE_STDIO
131
        void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
132
 
133
                const portCHAR * const pcTaskStartMsg = "Alt primary block time test started.\r\n";
134
 
135
                /* Queue a message for printing to say the task has started. */
136
                vPrintDisplayMessage( &pcTaskStartMsg );
137
        #endif
138
 
139
        ( void ) pvParameters;
140
 
141
        for( ;; )
142
        {
143
                /*********************************************************************
144
        Test 1
145
 
146
        Simple block time wakeup test on queue receives. */
147
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
148
                {
149
                        /* The queue is empty. Attempt to read from the queue using a block
150
                        time.  When we wake, ensure the delta in time is as expected. */
151
                        xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
152
 
153
                        /* A critical section is used to minimise the jitter in the time
154
                        measurements. */
155
                        portENTER_CRITICAL();
156
                        {
157
                                xTimeWhenBlocking = xTaskGetTickCount();
158
 
159
                                /* We should unblock after xTimeToBlock having not received
160
                                anything on the queue. */
161
                                if( xQueueAltReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
162
                                {
163
                                        xErrorOccurred = pdTRUE;
164
                                }
165
 
166
                                /* How long were we blocked for? */
167
                                xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
168
                        }
169
                        portEXIT_CRITICAL();
170
 
171
                        if( xBlockedTime < xTimeToBlock )
172
                        {
173
                                /* Should not have blocked for less than we requested. */
174
                                xErrorOccurred = pdTRUE;
175
                        }
176
 
177
                        if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
178
                        {
179
                                /* Should not have blocked for longer than we requested,
180
                                although we would not necessarily run as soon as we were
181
                                unblocked so a margin is allowed. */
182
                                xErrorOccurred = pdTRUE;
183
                        }
184
                }
185
 
186
 
187
                #if configUSE_PREEMPTION == 0
188
                        taskYIELD();
189
                #endif
190
 
191
 
192
                /*********************************************************************
193
        Test 2
194
 
195
        Simple block time wakeup test on queue sends.
196
 
197
                First fill the queue.  It should be empty so all sends should pass. */
198
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
199
                {
200
                        if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
201
                        {
202
                                xErrorOccurred = pdTRUE;
203
                        }
204
                }
205
 
206
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
207
                {
208
                        /* The queue is full. Attempt to write to the queue using a block
209
                        time.  When we wake, ensure the delta in time is as expected. */
210
                        xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
211
 
212
                        portENTER_CRITICAL();
213
                        {
214
                                xTimeWhenBlocking = xTaskGetTickCount();
215
 
216
                                /* We should unblock after xTimeToBlock having not received
217
                                anything on the queue. */
218
                                if( xQueueAltSendToBack( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
219
                                {
220
                                        xErrorOccurred = pdTRUE;
221
                                }
222
 
223
                                /* How long were we blocked for? */
224
                                xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
225
                        }
226
                        portEXIT_CRITICAL();
227
 
228
                        if( xBlockedTime < xTimeToBlock )
229
                        {
230
                                /* Should not have blocked for less than we requested. */
231
                                xErrorOccurred = pdTRUE;
232
                        }
233
 
234
                        if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
235
                        {
236
                                /* Should not have blocked for longer than we requested,
237
                                although we would not necessarily run as soon as we were
238
                                unblocked so a margin is allowed. */
239
                                xErrorOccurred = pdTRUE;
240
                        }
241
                }
242
 
243
                #if configUSE_PREEMPTION == 0
244
                        taskYIELD();
245
                #endif
246
 
247
 
248
                /*********************************************************************
249
        Test 3
250
 
251
                Wake the other task, it will block attempting to post to the queue.
252
                When we read from the queue the other task will wake, but before it
253
                can run we will post to the queue again.  When the other task runs it
254
                will find the queue still full, even though it was woken.  It should
255
                recognise that its block time has not expired and return to block for
256
                the remains of its block time.
257
 
258
                Wake the other task so it blocks attempting to post to the already
259
                full queue. */
260
                xRunIndicator = 0;
261
                vTaskResume( xSecondary );
262
 
263
                /* We need to wait a little to ensure the other task executes. */
264
                while( xRunIndicator != bktRUN_INDICATOR )
265
                {
266
                        /* The other task has not yet executed. */
267
                        vTaskDelay( bktSHORT_WAIT );
268
                }
269
                /* Make sure the other task is blocked on the queue. */
270
                vTaskDelay( bktSHORT_WAIT );
271
                xRunIndicator = 0;
272
 
273
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
274
                {
275
                        /* Now when we make space on the queue the other task should wake
276
                        but not execute as this task has higher priority. */
277
                        if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
278
                        {
279
                                xErrorOccurred = pdTRUE;
280
                        }
281
 
282
                        /* Now fill the queue again before the other task gets a chance to
283
                        execute.  If the other task had executed we would find the queue
284
                        full ourselves, and the other task have set xRunIndicator. */
285
                        if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
286
                        {
287
                                xErrorOccurred = pdTRUE;
288
                        }
289
 
290
                        if( xRunIndicator == bktRUN_INDICATOR )
291
                        {
292
                                /* The other task should not have executed. */
293
                                xErrorOccurred = pdTRUE;
294
                        }
295
 
296
                        /* Raise the priority of the other task so it executes and blocks
297
                        on the queue again. */
298
                        vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
299
 
300
                        /* The other task should now have re-blocked without exiting the
301
                        queue function. */
302
                        if( xRunIndicator == bktRUN_INDICATOR )
303
                        {
304
                                /* The other task should not have executed outside of the
305
                                queue function. */
306
                                xErrorOccurred = pdTRUE;
307
                        }
308
 
309
                        /* Set the priority back down. */
310
                        vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
311
                }
312
 
313
                /* Let the other task timeout.  When it unblockes it will check that it
314
                unblocked at the correct time, then suspend itself. */
315
                while( xRunIndicator != bktRUN_INDICATOR )
316
                {
317
                        vTaskDelay( bktSHORT_WAIT );
318
                }
319
                vTaskDelay( bktSHORT_WAIT );
320
                xRunIndicator = 0;
321
 
322
                #if configUSE_PREEMPTION == 0
323
                        taskYIELD();
324
                #endif
325
 
326
                /*********************************************************************
327
        Test 4
328
 
329
                As per test 3 - but with the send and receive the other way around.
330
                The other task blocks attempting to read from the queue.
331
 
332
                Empty the queue.  We should find that it is full. */
333
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
334
                {
335
                        if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
336
                        {
337
                                xErrorOccurred = pdTRUE;
338
                        }
339
                }
340
 
341
                /* Wake the other task so it blocks attempting to read from  the
342
                already empty queue. */
343
                vTaskResume( xSecondary );
344
 
345
                /* We need to wait a little to ensure the other task executes. */
346
                while( xRunIndicator != bktRUN_INDICATOR )
347
                {
348
                        vTaskDelay( bktSHORT_WAIT );
349
                }
350
                vTaskDelay( bktSHORT_WAIT );
351
                xRunIndicator = 0;
352
 
353
                for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
354
                {
355
                        /* Now when we place an item on the queue the other task should
356
                        wake but not execute as this task has higher priority. */
357
                        if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
358
                        {
359
                                xErrorOccurred = pdTRUE;
360
                        }
361
 
362
                        /* Now empty the queue again before the other task gets a chance to
363
                        execute.  If the other task had executed we would find the queue
364
                        empty ourselves, and the other task would be suspended. */
365
                        if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
366
                        {
367
                                xErrorOccurred = pdTRUE;
368
                        }
369
 
370
                        if( xRunIndicator == bktRUN_INDICATOR )
371
                        {
372
                                /* The other task should not have executed. */
373
                                xErrorOccurred = pdTRUE;
374
                        }
375
 
376
                        /* Raise the priority of the other task so it executes and blocks
377
                        on the queue again. */
378
                        vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
379
 
380
                        /* The other task should now have re-blocked without exiting the
381
                        queue function. */
382
                        if( xRunIndicator == bktRUN_INDICATOR )
383
                        {
384
                                /* The other task should not have executed outside of the
385
                                queue function. */
386
                                xErrorOccurred = pdTRUE;
387
                        }
388
                        vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
389
                }
390
 
391
                /* Let the other task timeout.  When it unblockes it will check that it
392
                unblocked at the correct time, then suspend itself. */
393
                while( xRunIndicator != bktRUN_INDICATOR )
394
                {
395
                        vTaskDelay( bktSHORT_WAIT );
396
                }
397
                vTaskDelay( bktSHORT_WAIT );
398
 
399
                xPrimaryCycles++;
400
        }
401
}
402
/*-----------------------------------------------------------*/
403
 
404
static void vSecondaryBlockTimeTestTask( void *pvParameters )
405
{
406
portTickType xTimeWhenBlocking, xBlockedTime;
407
portBASE_TYPE xData;
408
 
409
        #ifdef USE_STDIO
410
        void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
411
 
412
                const portCHAR * const pcTaskStartMsg = "Alt secondary block time test started.\r\n";
413
 
414
                /* Queue a message for printing to say the task has started. */
415
                vPrintDisplayMessage( &pcTaskStartMsg );
416
        #endif
417
 
418
        ( void ) pvParameters;
419
 
420
        for( ;; )
421
        {
422
                /*********************************************************************
423
        Test 1 and 2
424
 
425
                This task does does not participate in these tests. */
426
                vTaskSuspend( NULL );
427
 
428
                /*********************************************************************
429
        Test 3
430
 
431
                The first thing we do is attempt to read from the queue.  It should be
432
                full so we block.  Note the time before we block so we can check the
433
                wake time is as per that expected. */
434
                portENTER_CRITICAL();
435
                {
436
                        xTimeWhenBlocking = xTaskGetTickCount();
437
 
438
                        /* We should unblock after bktTIME_TO_BLOCK having not received
439
                        anything on the queue. */
440
                        xData = 0;
441
                        xRunIndicator = bktRUN_INDICATOR;
442
                        if( xQueueAltSendToBack( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
443
                        {
444
                                xErrorOccurred = pdTRUE;
445
                        }
446
 
447
                        /* How long were we inside the send function? */
448
                        xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
449
                }
450
                portEXIT_CRITICAL();
451
 
452
                /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
453
                if( xBlockedTime < bktTIME_TO_BLOCK )
454
                {
455
                        xErrorOccurred = pdTRUE;
456
                }
457
 
458
                /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
459
                either.  A margin is permitted as we would not necessarily run as
460
                soon as we unblocked. */
461
                if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
462
                {
463
                        xErrorOccurred = pdTRUE;
464
                }
465
 
466
                /* Suspend ready for test 3. */
467
                xRunIndicator = bktRUN_INDICATOR;
468
                vTaskSuspend( NULL );
469
 
470
                /*********************************************************************
471
        Test 4
472
 
473
                As per test three, but with the send and receive reversed. */
474
                portENTER_CRITICAL();
475
                {
476
                        xTimeWhenBlocking = xTaskGetTickCount();
477
 
478
                        /* We should unblock after bktTIME_TO_BLOCK having not received
479
                        anything on the queue. */
480
                        xRunIndicator = bktRUN_INDICATOR;
481
                        if( xQueueAltReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
482
                        {
483
                                xErrorOccurred = pdTRUE;
484
                        }
485
 
486
                        xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
487
                }
488
                portEXIT_CRITICAL();
489
 
490
                /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
491
                if( xBlockedTime < bktTIME_TO_BLOCK )
492
                {
493
                        xErrorOccurred = pdTRUE;
494
                }
495
 
496
                /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
497
                either.  A margin is permitted as we would not necessarily run as soon
498
                as we unblocked. */
499
                if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
500
                {
501
                        xErrorOccurred = pdTRUE;
502
                }
503
 
504
                xRunIndicator = bktRUN_INDICATOR;
505
 
506
                xSecondaryCycles++;
507
        }
508
}
509
/*-----------------------------------------------------------*/
510
 
511
portBASE_TYPE xAreAltBlockTimeTestTasksStillRunning( void )
512
{
513
static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
514
portBASE_TYPE xReturn = pdPASS;
515
 
516
        /* Have both tasks performed at least one cycle since this function was
517
        last called? */
518
        if( xPrimaryCycles == xLastPrimaryCycleCount )
519
        {
520
                xReturn = pdFAIL;
521
        }
522
 
523
        if( xSecondaryCycles == xLastSecondaryCycleCount )
524
        {
525
                xReturn = pdFAIL;
526
        }
527
 
528
        if( xErrorOccurred == pdTRUE )
529
        {
530
                xReturn = pdFAIL;
531
        }
532
 
533
        xLastSecondaryCycleCount = xSecondaryCycles;
534
        xLastPrimaryCycleCount = xPrimaryCycles;
535
 
536
        return xReturn;
537
}

powered by: WebSVN 2.1.0

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