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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 577 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
        NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
56
        The processor MUST be in supervisor mode when vTaskStartScheduler is
57
        called.  The demo applications included in the FreeRTOS.org download switch
58
        to supervisor mode prior to main being called.  If you are not using one of
59
        these demo application projects then ensure Supervisor mode is used.
60
*/
61
 
62
 
63
/*
64
 * Creates all the demo application tasks, then starts the scheduler.  The WEB
65
 * documentation provides more details of the demo application tasks.
66
 *
67
 * Main.c also creates a task called "Check".  This only executes every three
68
 * seconds but has the highest priority so is guaranteed to get processor time.
69
 * Its main function is to check that all the other tasks are still operational.
70
 * Each task (other than the "flash" tasks) maintains a unique count that is
71
 * incremented each time the task successfully completes its function.  Should
72
 * any error occur within such a task the count is permanently halted.  The
73
 * check task inspects the count of each task to ensure it has changed since
74
 * the last time the check task executed.  If all the count variables have
75
 * changed all the tasks are still executing error free, and the check task
76
 * toggles the onboard LED.  Should any task contain an error at any time
77
 * the LED toggle rate will change from 3 seconds to 500ms.
78
 *
79
 * To check the operation of the memory allocator the check task also
80
 * dynamically creates a task before delaying, and deletes it again when it
81
 * wakes.  If memory cannot be allocated for the new task the call to xTaskCreate
82
 * will fail and an error is signalled.  The dynamically created task itself
83
 * allocates and frees memory just to give the allocator a bit more exercise.
84
 *
85
 */
86
 
87
/* Standard includes. */
88
#include <stdlib.h>
89
#include <string.h>
90
 
91
/* Scheduler includes. */
92
#include "FreeRTOS.h"
93
#include "task.h"
94
 
95
/* Demo application includes. */
96
#include "partest.h"
97
#include "flash.h"
98
#include "integer.h"
99
#include "PollQ.h"
100
#include "comtest2.h"
101
#include "semtest.h"
102
#include "flop.h"
103
#include "dynamic.h"
104
#include "BlockQ.h"
105
#include "serial.h"
106
 
107
/* Hardware specific definitions. */
108
#include "aic.h"
109
#include "ebi.h"
110
 
111
/*-----------------------------------------------------------*/
112
 
113
/* Constants for the ComTest tasks. */
114
#define mainCOM_TEST_BAUD_RATE  ( ( unsigned long ) 115200 )
115
#define mainCOM_TEST_LED                ( 5 )
116
 
117
/* Priorities for the demo application tasks. */
118
#define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )
119
#define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
120
#define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
121
#define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )
122
#define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
123
#define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
124
 
125
/* The rate at which the on board LED will toggle when there is/is not an
126
error. */
127
#define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )
128
#define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )
129
#define mainON_BOARD_LED_BIT            ( ( unsigned long ) 7 )
130
 
131
/* Constants used by the vMemCheckTask() task. */
132
#define mainCOUNT_INITIAL_VALUE         ( ( unsigned long ) 0 )
133
#define mainNO_TASK                                     ( 0 )
134
 
135
/* The size of the memory blocks allocated by the vMemCheckTask() task. */
136
#define mainMEM_CHECK_SIZE_1            ( ( size_t ) 51 )
137
#define mainMEM_CHECK_SIZE_2            ( ( size_t ) 52 )
138
#define mainMEM_CHECK_SIZE_3            ( ( size_t ) 151 )
139
 
140
#define MAX_WAIT_STATES  8
141
static const unsigned long ululCSRWaitValues[ MAX_WAIT_STATES + 1 ] =
142
{
143
        WaitState1,/* There is no "zero wait state" value, so use one wait state */
144
        WaitState1,
145
        WaitState2,
146
        WaitState3,
147
        WaitState4,
148
        WaitState5,
149
        WaitState6,
150
        WaitState7,
151
        WaitState8
152
};
153
/*-----------------------------------------------------------*/
154
 
155
/*
156
 * Checks that all the demo application tasks are still executing without error
157
 * - as described at the top of the file.
158
 */
159
static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
160
 
161
/*
162
 * The task that executes at the highest priority and calls
163
 * prvCheckOtherTasksAreStillRunning().  See the description at the top
164
 * of the file.
165
 */
166
static void vErrorChecks( void *pvParameters );
167
 
168
/*
169
 * Dynamically created and deleted during each cycle of the vErrorChecks()
170
 * task.  This is done to check the operation of the memory allocator.
171
 * See the top of vErrorChecks for more details.
172
 */
173
static void vMemCheckTask( void *pvParameters );
174
 
175
/*
176
 * Configure the processor for use with the Olimex demo board.  This includes
177
 * setup for the I/O, system clock, and access timings.
178
 */
179
static void prvSetupHardware( void );
180
 
181
/*-----------------------------------------------------------*/
182
 
183
/*
184
 * Starts all the other tasks, then starts the scheduler.
185
 */
186
int main( void )
187
{
188
        /* Setup the hardware for use with the Olimex demo board. */
189
        prvSetupHardware();
190
 
191
        /* Start the demo/test application tasks. */
192
        vStartIntegerMathTasks( tskIDLE_PRIORITY );
193
        vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
194
        vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
195
        vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
196
        vStartMathTasks( tskIDLE_PRIORITY );
197
        vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
198
        vStartDynamicPriorityTasks();
199
        vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
200
 
201
        /* Start the check task - which is defined in this file. */
202
        xTaskCreate( vErrorChecks, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
203
 
204
        /* Now all the tasks have been started - start the scheduler.
205
 
206
        NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
207
        The processor MUST be in supervisor mode when vTaskStartScheduler is
208
        called.  The demo applications included in the FreeRTOS.org download switch
209
        to supervisor mode prior to main being called.  If you are not using one of
210
        these demo application projects then ensure Supervisor mode is used here. */
211
        vTaskStartScheduler();
212
 
213
        /* Should never reach here! */
214
        return 0;
215
}
216
/*-----------------------------------------------------------*/
217
 
218
static void vErrorChecks( void *pvParameters )
219
{
220
portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
221
unsigned long ulMemCheckTaskRunningCount;
222
xTaskHandle xCreatedTask;
223
 
224
        /* Just to stop compiler warnings. */
225
        ( void ) pvParameters;
226
 
227
        /* Cycle for ever, delaying then checking all the other tasks are still
228
        operating without error.  If an error is detected then the delay period
229
        is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
230
        the on board LED flash rate will increase.
231
 
232
        In addition to the standard tests the memory allocator is tested through
233
        the dynamic creation and deletion of a task each cycle.  Each time the
234
        task is created memory must be allocated for its stack.  When the task is
235
        deleted this memory is returned to the heap.  If the task cannot be created
236
        then it is likely that the memory allocation failed. */
237
 
238
        for( ;; )
239
        {
240
                /* Reset xCreatedTask.  This is modified by the task about to be
241
                created so we can tell if it is executing correctly or not. */
242
                xCreatedTask = mainNO_TASK;
243
 
244
                /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
245
                parameter. */
246
                ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
247
                if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
248
                {
249
                        /* Could not create the task - we have probably run out of heap. */
250
                        xDelayPeriod = mainERROR_FLASH_PERIOD;
251
                }
252
 
253
                /* Delay until it is time to execute again. */
254
                vTaskDelay( xDelayPeriod );
255
 
256
                /* Delete the dynamically created task. */
257
                if( xCreatedTask != mainNO_TASK )
258
                {
259
                        vTaskDelete( xCreatedTask );
260
                }
261
 
262
                /* Check all the standard demo application tasks are executing without
263
                error.  ulMemCheckTaskRunningCount is checked to ensure it was
264
                modified by the task just deleted. */
265
                if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
266
                {
267
                        /* An error has been detected in one of the tasks - flash faster. */
268
                        xDelayPeriod = mainERROR_FLASH_PERIOD;
269
                }
270
 
271
                /* The toggle rate of the LED depends on how long this task delays for.
272
                An error reduces the delay period and so increases the toggle rate. */
273
                vParTestToggleLED( mainON_BOARD_LED_BIT );
274
        }
275
}
276
/*-----------------------------------------------------------*/
277
 
278
static void prvSetupHardware( void )
279
{
280
long lCount;
281
 
282
        #ifdef RUN_FROM_ROM
283
        {
284
        portFLOAT nsecsPerClockTick;
285
        long lNumWaitStates;
286
        unsigned long ulCSRWaitValue;
287
 
288
                /* We are compiling to run from ROM (either on-chip or off-chip flash).
289
                Leave the RAM/flash mapped the way they are on reset
290
                (flash @ 0x00000000, RAM @ 0x00300000), and set up the
291
                proper flash wait states (starts out at the maximum number
292
                of wait states on reset, so we should be able to reduce it).
293
                Most of this code will probably get removed by the compiler
294
                if optimization is enabled, since these calculations are
295
                based on constants.  But the compiler should still produce
296
                a correct wait state register value. */
297
                nsecsPerClockTick = ( portFLOAT ) 1000000000 / configCPU_CLOCK_HZ;
298
                lNumWaitStates = ( long )( ( configFLASH_SPEED_NSEC / nsecsPerClockTick ) + 0.5 ) - 1;
299
 
300
                if( lNumWaitStates < 0 )
301
                {
302
                        lNumWaitStates = 0;
303
                }
304
 
305
                if( lNumWaitStates > MAX_WAIT_STATES )
306
                {
307
                        lNumWaitStates = MAX_WAIT_STATES;
308
                }
309
 
310
                ulCSRWaitValue = ululCSRWaitValues[ lNumWaitStates ];
311
                ulCSRWaitValue = WaitState5;
312
 
313
                AT91C_BASE_EBI->EBI_CSR[ 0 ] = ulCSRWaitValue | DataBus16 | WaitStateEnable
314
                                                                           | PageSize1M | tDF_0cycle
315
                                                                           | ByteWriteAccessType | CSEnable
316
                                                                           | 0x00000000 /* Base Address */;
317
        }
318
        #else  /* else we are compiling to run from on-chip RAM */
319
        {
320
                /* If compiling to run from RAM, we expect the on-chip RAM to already
321
                be mapped at 0x00000000.  This is typically done with an initialization
322
                script for the JTAG emulator you are using to download and run the
323
                demo application. So there is nothing to do here in this case. */
324
        }
325
        #endif
326
 
327
        /* Disable all interrupts at the AIC level initially... */
328
        AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;
329
 
330
        /* Set all SVR and SMR entries to default values (start with a clean slate)... */
331
        for( lCount = 0; lCount < 32; lCount++ )
332
        {
333
                AT91C_BASE_AIC->AIC_SVR[ lCount ] = (unsigned long) 0;
334
                AT91C_BASE_AIC->AIC_SMR[ lCount ] = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
335
        }
336
 
337
        /* Disable clocks to all peripherals initially... */
338
        AT91C_BASE_PS->PS_PCDR = 0xFFFFFFFF;
339
 
340
        /* Clear all interrupts at the AIC level initially... */
341
        AT91C_BASE_AIC->AIC_ICCR = 0xFFFFFFFF;
342
 
343
        /* Perform 8 "End Of Interrupt" cmds to make sure AIC will not Lock out
344
        nIRQ */
345
        for( lCount = 0; lCount < 8; lCount++ )
346
        {
347
                AT91C_BASE_AIC->AIC_EOICR = 0;
348
        }
349
 
350
        /* Initialise LED outputs. */
351
        vParTestInitialise();
352
}
353
/*-----------------------------------------------------------*/
354
 
355
static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
356
{
357
long lReturn = ( long ) pdPASS;
358
 
359
        /* Check all the demo tasks (other than the flash tasks) to ensure
360
        that they are all still running, and that none of them have detected
361
        an error. */
362
 
363
        if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
364
        {
365
                lReturn = ( long ) pdFAIL;
366
        }
367
 
368
        if( xAreComTestTasksStillRunning() != pdTRUE )
369
        {
370
                lReturn = ( long ) pdFAIL;
371
        }
372
 
373
        if( xArePollingQueuesStillRunning() != pdTRUE )
374
        {
375
                lReturn = ( long ) pdFAIL;
376
        }
377
 
378
        if( xAreMathsTaskStillRunning() != pdTRUE )
379
        {
380
                lReturn = ( long ) pdFAIL;
381
        }
382
 
383
        if( xAreSemaphoreTasksStillRunning() != pdTRUE )
384
        {
385
                lReturn = ( long ) pdFAIL;
386
        }
387
 
388
        if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
389
        {
390
                lReturn = ( long ) pdFAIL;
391
        }
392
 
393
        if( xAreBlockingQueuesStillRunning() != pdTRUE )
394
        {
395
                lReturn = ( long ) pdFAIL;
396
        }
397
 
398
        if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
399
        {
400
                /* The vMemCheckTask did not increment the counter - it must
401
                have failed. */
402
                lReturn = ( long ) pdFAIL;
403
        }
404
 
405
        return lReturn;
406
}
407
/*-----------------------------------------------------------*/
408
 
409
static void vMemCheckTask( void *pvParameters )
410
{
411
unsigned long *pulMemCheckTaskRunningCounter;
412
void *pvMem1, *pvMem2, *pvMem3;
413
static long lErrorOccurred = pdFALSE;
414
 
415
        /* This task is dynamically created then deleted during each cycle of the
416
        vErrorChecks task to check the operation of the memory allocator.  Each time
417
        the task is created memory is allocated for the stack and TCB.  Each time
418
        the task is deleted this memory is returned to the heap.  This task itself
419
        exercises the allocator by allocating and freeing blocks.
420
 
421
        The task executes at the idle priority so does not require a delay.
422
 
423
        pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
424
        vErrorChecks() task that this task is still executing without error. */
425
 
426
        pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
427
 
428
        for( ;; )
429
        {
430
                if( lErrorOccurred == pdFALSE )
431
                {
432
                        /* We have never seen an error so increment the counter. */
433
                        ( *pulMemCheckTaskRunningCounter )++;
434
                }
435
                else
436
                {
437
                        /* There has been an error so reset the counter so the check task
438
                        can tell that an error occurred. */
439
                        *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
440
                }
441
 
442
                /* Allocate some memory - just to give the allocator some extra
443
                exercise.  This has to be in a critical section to ensure the
444
                task does not get deleted while it has memory allocated. */
445
                vTaskSuspendAll();
446
                {
447
                        pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
448
                        if( pvMem1 == NULL )
449
                        {
450
                                lErrorOccurred = pdTRUE;
451
                        }
452
                        else
453
                        {
454
                                memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
455
                                vPortFree( pvMem1 );
456
                        }
457
                }
458
                xTaskResumeAll();
459
 
460
                /* Again - with a different size block. */
461
                vTaskSuspendAll();
462
                {
463
                        pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
464
                        if( pvMem2 == NULL )
465
                        {
466
                                lErrorOccurred = pdTRUE;
467
                        }
468
                        else
469
                        {
470
                                memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
471
                                vPortFree( pvMem2 );
472
                        }
473
                }
474
                xTaskResumeAll();
475
 
476
                /* Again - with a different size block. */
477
                vTaskSuspendAll();
478
                {
479
                        pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
480
                        if( pvMem3 == NULL )
481
                        {
482
                                lErrorOccurred = pdTRUE;
483
                        }
484
                        else
485
                        {
486
                                memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
487
                                vPortFree( pvMem3 );
488
                        }
489
                }
490
                xTaskResumeAll();
491
        }
492
}
493
 

powered by: WebSVN 2.1.0

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