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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [AVR32_UC3/] [main.c] - Blame information for rev 597

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

Line No. Rev Author Line
1 589 jeremybenn
/*This file has been prepared for Doxygen automatic documentation generation.*/
2
/*! \file *********************************************************************
3
 *
4
 * \brief FreeRTOS Real Time Kernel example.
5
 *
6
 * Creates all the demo application tasks, then starts the scheduler.  The WEB
7
 * documentation provides more details of the demo application tasks.
8
 *
9
 * Main. c also creates a task called "Check".  This only executes every three
10
 * seconds but has the highest priority so is guaranteed to get processor time.
11
 * Its main function is to check that all the other tasks are still operational.
12
 * Each task that does not flash an LED maintains a unique count that is
13
 * incremented each time the task successfully completes its function.  Should
14
 * any error occur within such a task the count is permanently halted.  The
15
 * check task inspects the count of each task to ensure it has changed since
16
 * the last time the check task executed.  If all the count variables have
17
 * changed all the tasks are still executing error free, and the check task
18
 * toggles an LED.  Should any task contain an error at any time the LED toggle
19
 * will stop.
20
 *
21
 * The LED flash and communications test tasks do not maintain a count.
22
 *
23
 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
24
 * - Supported devices:  All AVR32 devices with GPIO.
25
 * - AppNote:
26
 *
27
 * \author               Atmel Corporation: http://www.atmel.com \n
28
 *                       Support and FAQ: http://support.atmel.no/
29
 *
30
 *****************************************************************************/
31
 
32
/*
33
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
34
 
35
    ***************************************************************************
36
    *                                                                         *
37
    * If you are:                                                             *
38
    *                                                                         *
39
    *    + New to FreeRTOS,                                                   *
40
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
41
    *    + Looking for basic training,                                        *
42
    *    + Wanting to improve your FreeRTOS skills and productivity           *
43
    *                                                                         *
44
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
45
    *                                                                         *
46
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
47
    *                  http://www.FreeRTOS.org/Documentation                  *
48
    *                                                                         *
49
    * A pdf reference manual is also available.  Both are usually delivered   *
50
    * to your inbox within 20 minutes to two hours when purchased between 8am *
51
    * and 8pm GMT (although please allow up to 24 hours in case of            *
52
    * exceptional circumstances).  Thank you for your support!                *
53
    *                                                                         *
54
    ***************************************************************************
55
 
56
    This file is part of the FreeRTOS distribution.
57
 
58
    FreeRTOS is free software; you can redistribute it and/or modify it under
59
    the terms of the GNU General Public License (version 2) as published by the
60
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
61
    ***NOTE*** The exception to the GPL is included to allow you to distribute
62
    a combined work that includes FreeRTOS without being obliged to provide the
63
    source code for proprietary components outside of the FreeRTOS kernel.
64
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
65
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
66
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
67
    more details. You should have received a copy of the GNU General Public
68
    License and the FreeRTOS license exception along with FreeRTOS; if not it
69
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
70
    by writing to Richard Barry, contact details for whom are available on the
71
    FreeRTOS WEB site.
72
 
73
    1 tab == 4 spaces!
74
 
75
    http://www.FreeRTOS.org - Documentation, latest information, license and
76
    contact details.
77
 
78
    http://www.SafeRTOS.com - A version that is certified for use in safety
79
    critical systems.
80
 
81
    http://www.OpenRTOS.com - Commercial support, development, porting,
82
    licensing and training services.
83
*/
84
 
85
 
86
#include <stdio.h>
87
#include <stdlib.h>
88
#include <string.h>
89
 
90
/* Environment header files. */
91
#include "pm.h"
92
 
93
/* Scheduler header files. */
94
#include "FreeRTOS.h"
95
#include "task.h"
96
 
97
/* Demo file headers. */
98
#include "partest.h"
99
#include "serial.h"
100
#include "integer.h"
101
#include "comtest.h"
102
#include "flash.h"
103
#include "PollQ.h"
104
#include "semtest.h"
105
#include "dynamic.h"
106
#include "BlockQ.h"
107
#include "death.h"
108
#include "flop.h"
109
 
110
/*! \name Priority definitions for most of the tasks in the demo application.
111
 * Some tasks just use the idle priority.
112
 */
113
//! @{
114
#define mainLED_TASK_PRIORITY     ( tskIDLE_PRIORITY + 1 )
115
#define mainCOM_TEST_PRIORITY     ( tskIDLE_PRIORITY + 2 )
116
#define mainQUEUE_POLL_PRIORITY   ( tskIDLE_PRIORITY + 2 )
117
#define mainSEM_TEST_PRIORITY     ( tskIDLE_PRIORITY + 1 )
118
#define mainBLOCK_Q_PRIORITY      ( tskIDLE_PRIORITY + 3 )
119
#define mainCHECK_TASK_PRIORITY   ( tskIDLE_PRIORITY + 4 )
120
#define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
121
//! @}
122
 
123
//! Baud rate used by the serial port tasks.
124
#define mainCOM_TEST_BAUD_RATE    ( ( unsigned portLONG ) 57600 )
125
 
126
//! LED used by the serial port tasks.  This is toggled on each character Tx,
127
//! and mainCOM_TEST_LED + 1 is toggled on each character Rx.
128
#define mainCOM_TEST_LED          ( 3 )
129
 
130
//! LED that is toggled by the check task.  The check task periodically checks
131
//! that all the other tasks are operating without error.  If no errors are found
132
//! the LED is toggled.  If an error is found at any time the LED toggles faster.
133
#define mainCHECK_TASK_LED        ( 6 )
134
 
135
//! LED that is set upon error.
136
#define mainERROR_LED             ( 7 )
137
 
138
//! The period between executions of the check task.
139
#define mainCHECK_PERIOD          ( ( portTickType ) 3000 / portTICK_RATE_MS  )
140
 
141
//! If an error is detected in a task, the vErrorChecks task will enter in an
142
//! infinite loop flashing the LED at this rate.
143
#define mainERROR_FLASH_RATE      ( (portTickType) 500 / portTICK_RATE_MS )
144
 
145
/*! \name Constants used by the vMemCheckTask() task.
146
 */
147
//! @{
148
#define mainCOUNT_INITIAL_VALUE   ( ( unsigned portLONG ) 0 )
149
#define mainNO_TASK               ( 0 )
150
//! @}
151
 
152
/*! \name The size of the memory blocks allocated by the vMemCheckTask() task.
153
 */
154
//! @{
155
#define mainMEM_CHECK_SIZE_1      ( ( size_t ) 51 )
156
#define mainMEM_CHECK_SIZE_2      ( ( size_t ) 52 )
157
#define mainMEM_CHECK_SIZE_3      ( ( size_t ) 15 )
158
//! @}
159
 
160
 
161
/*-----------------------------------------------------------*/
162
 
163
/*
164
 * The task that executes at the highest priority and calls
165
 * prvCheckOtherTasksAreStillRunning().  See the description at the top
166
 * of the file.
167
 */
168
static void vErrorChecks( void *pvParameters );
169
 
170
/*
171
 * Checks that all the demo application tasks are still executing without error
172
 * - as described at the top of the file.
173
 */
174
static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );
175
 
176
/*
177
 * A task that exercises the memory allocator.
178
 */
179
static void vMemCheckTask( void *pvParameters );
180
 
181
/*
182
 * Called by the check task following the detection of an error to set the
183
 * LEDs into a state that shows an error has beeen found.
184
 */
185
static void prvIndicateError( void );
186
 
187
/*-----------------------------------------------------------*/
188
 
189
int main( void )
190
{
191
        /* Start the crystal oscillator 0 and switch the main clock to it. */
192
        pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
193
 
194
        portDBG_TRACE("Starting the FreeRTOS AVR32 UC3 Demo...");
195
 
196
        /* Setup the LED's for output. */
197
        vParTestInitialise();
198
 
199
        /* Start the standard demo tasks.  See the WEB documentation for more
200
        information. */
201
        vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
202
        vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
203
        vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
204
        vStartIntegerMathTasks( tskIDLE_PRIORITY );
205
        vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
206
        vStartDynamicPriorityTasks();
207
        vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
208
        vStartMathTasks( tskIDLE_PRIORITY );
209
 
210
        /* Start the demo tasks defined within this file, specifically the check
211
        task as described at the top of this file. */
212
        xTaskCreate(
213
                vErrorChecks
214
                ,  (const signed portCHAR *)"ErrCheck"
215
                ,  configMINIMAL_STACK_SIZE
216
                ,  NULL
217
                ,  mainCHECK_TASK_PRIORITY
218
                ,  NULL );
219
 
220
        /* Start the scheduler. */
221
        vTaskStartScheduler();
222
 
223
        /* Will only get here if there was insufficient memory to create the idle
224
        task. */
225
 
226
        return 0;
227
}
228
/*-----------------------------------------------------------*/
229
 
230
/*!
231
 * \brief The task function for the "Check" task.
232
 */
233
static void vErrorChecks( void *pvParameters )
234
{
235
static volatile unsigned portLONG ulDummyVariable = 3UL;
236
unsigned portLONG ulMemCheckTaskRunningCount;
237
xTaskHandle xCreatedTask;
238
portBASE_TYPE bSuicidalTask = 0;
239
 
240
        /* The parameters are not used.  Prevent compiler warnings. */
241
        ( void ) pvParameters;
242
 
243
        /* Cycle for ever, delaying then checking all the other tasks are still
244
        operating without error.
245
 
246
        In addition to the standard tests the memory allocator is tested through
247
        the dynamic creation and deletion of a task each cycle.  Each time the
248
        task is created memory must be allocated for its stack.  When the task is
249
        deleted this memory is returned to the heap.  If the task cannot be created
250
        then it is likely that the memory allocation failed. */
251
 
252
        for( ;; )
253
        {
254
                /* Do this only once. */
255
                if( bSuicidalTask == 0 )
256
                {
257
                        bSuicidalTask++;
258
 
259
                        /* This task has to be created last as it keeps account of the number of
260
                        tasks it expects to see running. However its implementation expects
261
                        to be called before vTaskStartScheduler(). We're in the case here where
262
                        vTaskStartScheduler() has already been called (thus the hidden IDLE task
263
                        has already been spawned). Since vCreateSuicidalTask() supposes that the
264
                        IDLE task isn't included in the response from uxTaskGetNumberOfTasks(),
265
                        let the MEM_CHECK task play that role. => this is why vCreateSuicidalTasks()
266
                        is not called as the last task. */
267
                        vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
268
                }
269
 
270
                /* Reset xCreatedTask.  This is modified by the task about to be
271
                created so we can tell if it is executing correctly or not. */
272
                xCreatedTask = mainNO_TASK;
273
 
274
                /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
275
                parameter. */
276
                ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
277
 
278
                if( xTaskCreate( vMemCheckTask,
279
                        ( signed portCHAR * ) "MEM_CHECK",
280
                        configMINIMAL_STACK_SIZE,
281
                        ( void * ) &ulMemCheckTaskRunningCount,
282
                        tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
283
                {
284
                        /* Could not create the task - we have probably run out of heap.
285
                        Don't go any further and flash the LED faster to provide visual
286
                        feedback of the error. */
287
                        prvIndicateError();
288
                }
289
 
290
                /* Delay until it is time to execute again. */
291
                vTaskDelay( mainCHECK_PERIOD );
292
 
293
                /* Delete the dynamically created task. */
294
                if( xCreatedTask != mainNO_TASK )
295
                {
296
                        vTaskDelete( xCreatedTask );
297
                }
298
 
299
                /* Perform a bit of 32bit maths to ensure the registers used by the
300
                integer tasks get some exercise. The result here is not important -
301
                see the demo application documentation for more info. */
302
                ulDummyVariable *= 3;
303
 
304
                /* Check all other tasks are still operating without error.
305
                Check that vMemCheckTask did increment the counter. */
306
                if( ( prvCheckOtherTasksAreStillRunning() != pdFALSE )
307
                 || ( ulMemCheckTaskRunningCount == mainCOUNT_INITIAL_VALUE ) )
308
                {
309
                        /* An error has occurred in one of the tasks.
310
                        Don't go any further and flash the LED faster to give visual
311
                        feedback of the error. */
312
                        prvIndicateError();
313
                }
314
                else
315
                {
316
                        /* Toggle the LED if everything is okay. */
317
                        vParTestToggleLED( mainCHECK_TASK_LED );
318
                }
319
        }
320
}
321
/*-----------------------------------------------------------*/
322
 
323
 
324
/*!
325
 * \brief Checks that all the demo application tasks are still executing without error.
326
 */
327
static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )
328
{
329
static portBASE_TYPE xErrorHasOccurred = pdFALSE;
330
 
331
        if( xAreComTestTasksStillRunning() != pdTRUE )
332
        {
333
                xErrorHasOccurred = pdTRUE;
334
        }
335
 
336
        if( xArePollingQueuesStillRunning() != pdTRUE )
337
        {
338
                xErrorHasOccurred = pdTRUE;
339
        }
340
 
341
        if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
342
        {
343
                xErrorHasOccurred = pdTRUE;
344
        }
345
 
346
        if( xAreSemaphoreTasksStillRunning() != pdTRUE )
347
        {
348
                xErrorHasOccurred = pdTRUE;
349
        }
350
 
351
        if( xAreBlockingQueuesStillRunning() != pdTRUE )
352
        {
353
                xErrorHasOccurred = pdTRUE;
354
        }
355
 
356
        if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
357
        {
358
                xErrorHasOccurred = pdTRUE;
359
        }
360
 
361
        if( xAreMathsTaskStillRunning() != pdTRUE )
362
        {
363
                xErrorHasOccurred = pdTRUE;
364
        }
365
 
366
        if( xIsCreateTaskStillRunning() != pdTRUE )
367
        {
368
                xErrorHasOccurred = pdTRUE;
369
        }
370
 
371
        return ( xErrorHasOccurred );
372
}
373
/*-----------------------------------------------------------*/
374
 
375
 
376
/*!
377
 * \brief Dynamically created and deleted during each cycle of the vErrorChecks()
378
 * task.  This is done to check the operation of the memory allocator.
379
 * See the top of vErrorChecks for more details.
380
 *
381
 * \param *pvParameters Parameters for the task (can be of any kind)
382
 */
383
static void vMemCheckTask( void *pvParameters )
384
{
385
unsigned portLONG *pulMemCheckTaskRunningCounter;
386
void *pvMem1, *pvMem2, *pvMem3;
387
static portLONG lErrorOccurred = pdFALSE;
388
 
389
        /* This task is dynamically created then deleted during each cycle of the
390
        vErrorChecks task to check the operation of the memory allocator.  Each time
391
        the task is created memory is allocated for the stack and TCB.  Each time
392
        the task is deleted this memory is returned to the heap.  This task itself
393
        exercises the allocator by allocating and freeing blocks.
394
 
395
        The task executes at the idle priority so does not require a delay.
396
 
397
        pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
398
        vErrorChecks() task that this task is still executing without error. */
399
 
400
        pulMemCheckTaskRunningCounter = ( unsigned portLONG * ) pvParameters;
401
 
402
        for( ;; )
403
        {
404
                if( lErrorOccurred == pdFALSE )
405
                {
406
                        /* We have never seen an error so increment the counter. */
407
                        ( *pulMemCheckTaskRunningCounter )++;
408
                }
409
                else
410
                {
411
                        /* There has been an error so reset the counter so the check task
412
                        can tell that an error occurred. */
413
                        *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
414
                }
415
 
416
                /* Allocate some memory - just to give the allocator some extra
417
                exercise.  This has to be in a critical section to ensure the
418
                task does not get deleted while it has memory allocated. */
419
 
420
                vTaskSuspendAll();
421
                {
422
                        pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
423
 
424
                        if( pvMem1 == NULL )
425
                        {
426
                                lErrorOccurred = pdTRUE;
427
                        }
428
                        else
429
                        {
430
                                memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
431
                                vPortFree( pvMem1 );
432
                        }
433
                }
434
                xTaskResumeAll();
435
 
436
                /* Again - with a different size block. */
437
                vTaskSuspendAll();
438
                {
439
                        pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
440
 
441
                        if( pvMem2 == NULL )
442
                        {
443
                                lErrorOccurred = pdTRUE;
444
                        }
445
                        else
446
                        {
447
                                memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
448
                                vPortFree( pvMem2 );
449
                        }
450
                }
451
                xTaskResumeAll();
452
 
453
                /* Again - with a different size block. */
454
                vTaskSuspendAll();
455
                {
456
                        pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
457
                        if( pvMem3 == NULL )
458
                        {
459
                                lErrorOccurred = pdTRUE;
460
                        }
461
                        else
462
                        {
463
                                memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
464
                                vPortFree( pvMem3 );
465
                        }
466
                }
467
                xTaskResumeAll();
468
        }
469
}
470
/*-----------------------------------------------------------*/
471
 
472
static void prvIndicateError( void )
473
{
474
        /* The check task has found an error in one of the other tasks.
475
        Set the LEDs to a state that indicates this. */
476
        vParTestSetLED(mainERROR_LED,pdTRUE);
477
 
478
        for(;;)
479
        {
480
                #if( BOARD==EVK1100 )
481
                        vParTestToggleLED( mainCHECK_TASK_LED );
482
                        vTaskDelay( mainERROR_FLASH_RATE );
483
                #endif
484
                #if ( BOARD==EVK1101 )
485
                        vParTestSetLED( 0, pdTRUE );
486
                        vParTestSetLED( 1, pdTRUE );
487
                        vParTestSetLED( 2, pdTRUE );
488
                        vParTestSetLED( 3, pdTRUE );
489
                #endif
490
        }
491
}
492
 

powered by: WebSVN 2.1.0

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