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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_MPU_LPC1768_GCC_RedSuite/] [src/] [main.c] - Blame information for rev 582

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 582 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
#error "The batch file Demo\CORTEX_LPC1768_GCC_RedSuite\CreateProjectDirectoryStructure.bat must be executed before the first build.  After executing the batch file hit F5 to refrech the Eclipse project, then delete this line."
55
 
56
 
57
/*
58
 * This file demonstrates the use of FreeRTOS-MPU.  It creates tasks in both
59
 * User mode and Privileged mode, and using both the original xTaskCreate() and
60
 * the new xTaskCreateRestricted() API functions.  The purpose of each created
61
 * task is documented in the comments above the task function prototype (in
62
 * this file), with the task behaviour demonstrated and documented within the
63
 * task function itself.  In addition a queue is used to demonstrate passing
64
 * data between protected/restricted tasks as well as passing data between an
65
 * interrupt and a protected/restricted task.
66
 */
67
 
68
 
69
 
70
/* Library includes. */
71
#include <string.h>
72
 
73
/* Scheduler includes. */
74
#include "FreeRTOS.h"
75
#include "task.h"
76
#include "queue.h"
77
#include "semphr.h"
78
 
79
/* Red Suite includes. */
80
#include "lcd_driver.h"
81
#include "lcd.h"
82
 
83
 
84
/*-----------------------------------------------------------*/
85
 
86
/* Misc constants. */
87
#define mainDONT_BLOCK                                  ( 0 )
88
 
89
/* Definitions for the messages that can be sent to the check task. */
90
#define mainREG_TEST_1_STILL_EXECUTING  ( 0 )
91
#define mainREG_TEST_2_STILL_EXECUTING  ( 1 )
92
#define mainPRINT_SYSTEM_STATUS                 ( 2 )
93
 
94
/* GCC specifics. */
95
#define mainALIGN_TO( x )                               __attribute__((aligned(x)))
96
 
97
/* Hardware specifics.  The start and end address are chosen to ensure the
98
required GPIO are covered while also ensuring the necessary alignment is
99
achieved. */
100
#define mainGPIO_START_ADDRESS                  ( ( unsigned long * ) 0x2009c000 )
101
#define mainGPIO_END_ADDRESS                    ( mainGPIO_START_ADDRESS + ( 64 * 1024 ) )
102
 
103
 
104
/*-----------------------------------------------------------*/
105
/* Prototypes for functions that implement tasks. -----------*/
106
/*-----------------------------------------------------------*/
107
 
108
/*
109
 * Prototype for the reg test tasks.  Amongst other things, these fill the CPU
110
 * registers with known values before checking that the registers still contain
111
 * the expected values.  Each of the two tasks use different values so an error
112
 * in the context switch mechanism can be caught.  Both reg test tasks execute
113
 * at the idle priority so will get preempted regularly.  Each task repeatedly
114
 * sends a message on a queue so long as it remains functioning correctly.  If
115
 * an error is detected within the task the task is simply deleted.
116
 */
117
static void prvRegTest1Task( void *pvParameters );
118
static void prvRegTest2Task( void *pvParameters );
119
 
120
/*
121
 * Prototype for the check task.  The check task demonstrates various features
122
 * of the MPU before entering a loop where it waits for messages to arrive on a
123
 * queue.
124
 *
125
 * Two types of messages can be processes:
126
 *
127
 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
128
 *    task is still operational.
129
 *
130
 * 2) "Print Status commands" sent periodically by the tick hook function (and
131
 *    therefore from within an interrupt) which command the check task to write
132
 *    either pass or fail to the terminal, depending on the status of the reg
133
 *    test tasks.
134
 */
135
static void prvCheckTask( void *pvParameters );
136
 
137
/*
138
 * Prototype for a task created in User mode using the original vTaskCreate()
139
 * API function.  The task demonstrates the characteristics of such a task,
140
 * before simply deleting itself.
141
 */
142
static void prvOldStyleUserModeTask( void *pvParameters );
143
 
144
/*
145
 * Prototype for a task created in Privileged mode using the original
146
 * vTaskCreate() API function.  The task demonstrates the characteristics of
147
 * such a task, before simply deleting itself.
148
 */
149
static void prvOldStylePrivilegedModeTask( void *pvParameters );
150
 
151
 
152
/*-----------------------------------------------------------*/
153
/* Prototypes for other misc functions.  --------------------*/
154
/*-----------------------------------------------------------*/
155
 
156
/*
157
 * Just configures any clocks and IO necessary.
158
 */
159
static void prvSetupHardware( void );
160
 
161
/*
162
 * Simply deletes the calling task.  The function is provided only because it
163
 * is simpler to call from asm code than the normal vTaskDelete() API function.
164
 * It has the noinline attribute because it is called from asm code.
165
 */
166
static void prvDeleteMe( void ) __attribute__((noinline));
167
 
168
/*
169
 * Used by both reg test tasks to send messages to the check task.  The message
170
 * just lets the check task know that the task is still functioning correctly.
171
 * If a reg test task detects an error it will delete itself, and in so doing
172
 * prevent itself from sending any more 'I'm Alive' messages to the check task.
173
 */
174
static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
175
 
176
/*
177
 * The check task is created with access to three memory regions (plus its
178
 * stack).  Each memory region is configured with different parameters and
179
 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
180
 * region.  prvTestMemoryRegions() also demonstrates a task that was created
181
 * as a privileged task settings its own privilege level down to that of a user
182
 * task.
183
 */
184
static void prvTestMemoryRegions( void );
185
 
186
/*-----------------------------------------------------------*/
187
 
188
/* The handle of the queue used to communicate between tasks and between tasks
189
and interrupts.  Note that this is a file scope variable that falls outside of
190
any MPU region.  As such other techniques have to be used to allow the tasks
191
to gain access to the queue.  See the comments in the tasks themselves for
192
further information. */
193
static xQueueHandle xFileScopeCheckQueue = NULL;
194
 
195
 
196
 
197
/*-----------------------------------------------------------*/
198
/* Data used by the 'check' task. ---------------------------*/
199
/*-----------------------------------------------------------*/
200
 
201
/* Define the constants used to allocate the check task stack.  Note that the
202
stack size is defined in words, not bytes. */
203
#define mainCHECK_TASK_STACK_SIZE_WORDS 128
204
#define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
205
 
206
/* Declare the stack that will be used by the check task.  The kernel will
207
 automatically create an MPU region for the stack.  The stack alignment must
208
 match its size, so if 128 words are reserved for the stack then it must be
209
 aligned to ( 128 * 4 ) bytes. */
210
static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
211
 
212
/* Declare three arrays - an MPU region will be created for each array
213
using the xTaskParameters structure below.  THIS IS JUST TO DEMONSTRATE THE
214
MPU FUNCTIONALITY, the data is not used by the check tasks primary function
215
of monitoring the reg test tasks and printing out status information.
216
 
217
Note that the arrays allocate slightly more RAM than is actually assigned to
218
the MPU region.  This is to permit writes off the end of the array to be
219
detected even when the arrays are placed in adjacent memory locations (with no
220
gaps between them).  The align size must be a power of two. */
221
#define mainREAD_WRITE_ARRAY_SIZE 130
222
#define mainREAD_WRITE_ALIGN_SIZE 128
223
char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
224
 
225
#define mainREAD_ONLY_ARRAY_SIZE 260
226
#define mainREAD_ONLY_ALIGN_SIZE 256
227
char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
228
 
229
#define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
230
#define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
231
char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
232
 
233
/* Fill in a xTaskParameters structure to define the check task - this is the
234
structure passed to the xTaskCreateRestricted() function. */
235
static const xTaskParameters xCheckTaskParameters =
236
{
237
        prvCheckTask,                                                           /* pvTaskCode - the function that implements the task. */
238
        ( signed char * ) "Check",                                      /* pcName                       */
239
        mainCHECK_TASK_STACK_SIZE_WORDS,                        /* usStackDepth - defined in words, not bytes. */
240
        ( void * ) 0x12121212,                                          /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
241
        ( tskIDLE_PRIORITY + 1 ) | portPRIVILEGE_BIT,/* uxPriority - this is the highest priority task in the system.  The task is created in privileged mode to demonstrate accessing the privileged only data. */
242
        xCheckTaskStack,                                                        /* puxStackBuffer - the array to use as the task stack, as declared above. */
243
 
244
        /* xRegions - In this case the xRegions array is used to create MPU regions
245
        for all three of the arrays declared directly above.  Each MPU region is
246
        created with different parameters.  Again, THIS IS JUST TO DEMONSTRATE THE
247
        MPU FUNCTIONALITY, the data is not used by the check tasks primary function
248
        of monitoring the reg test tasks and printing out status information.*/
249
        {
250
                /* Base address                                 Length                                                                  Parameters */
251
        { cReadWriteArray,                              mainREAD_WRITE_ALIGN_SIZE,                              portMPU_REGION_READ_WRITE },
252
        { cReadOnlyArray,                               mainREAD_ONLY_ALIGN_SIZE,                               portMPU_REGION_READ_ONLY },
253
        { cPrivilegedOnlyAccessArray,   mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE,  portMPU_REGION_PRIVILEGED_READ_WRITE }
254
        }
255
};
256
 
257
/* Three MPU regions are defined for use by the 'check' task when the task is
258
created.  These are only used to demonstrate the MPU features and are not
259
actually necessary for the check task to fulfill its primary purpose.  Instead
260
the MPU regions are replaced with those defined by xAltRegions prior to the
261
check task receiving any data on the queue or printing any messages to the
262
debug console.  The MPU region defined below covers the GPIO peripherals used
263
to write to the LCD. */
264
static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
265
{
266
        /* Base address                         Length                  Parameters */
267
        { mainGPIO_START_ADDRESS,       ( 64 * 1024 ),  portMPU_REGION_READ_WRITE },
268
        { 0,                                             0,                               0 },
269
        { 0,                                             0,                               0 }
270
};
271
 
272
 
273
 
274
/*-----------------------------------------------------------*/
275
/* Data used by the 'reg test' tasks. -----------------------*/
276
/*-----------------------------------------------------------*/
277
 
278
/* Define the constants used to allocate the reg test task stacks.  Note that
279
that stack size is defined in words, not bytes. */
280
#define mainREG_TEST_STACK_SIZE_WORDS   128
281
#define mainREG_TEST_STACK_ALIGNMENT    ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
282
 
283
/* Declare the stacks that will be used by the reg test tasks.  The kernel will
284
automatically create an MPU region for the stack.  The stack alignment must
285
match its size, so if 128 words are reserved for the stack then it must be
286
aligned to ( 128 * 4 ) bytes. */
287
static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
288
static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
289
 
290
/* Fill in a xTaskParameters structure per reg test task to define the tasks. */
291
static const xTaskParameters xRegTest1Parameters =
292
{
293
        prvRegTest1Task,                                                /* pvTaskCode - the function that implements the task. */
294
        ( signed char * ) "RegTest1",                   /* pcName                       */
295
        mainREG_TEST_STACK_SIZE_WORDS,                  /* usStackDepth         */
296
        ( void * ) 0x12345678,                                  /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
297
        tskIDLE_PRIORITY | portPRIVILEGE_BIT,   /* uxPriority - note that this task is created with privileges to demonstrate one method of passing a queue handle into the task. */
298
        xRegTest1Stack,                                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */
299
        {                                                                               /* xRegions - this task does not use any non-stack data hence all members are zero. */
300
                /* Base address         Length          Parameters */
301
        { 0x00,                         0x00,                   0x00 },
302
        { 0x00,                         0x00,                   0x00 },
303
        { 0x00,                         0x00,                   0x00 }
304
        }
305
};
306
/*-----------------------------------------------------------*/
307
 
308
static xTaskParameters xRegTest2Parameters =
309
{
310
        prvRegTest2Task,                                /* pvTaskCode - the function that implements the task. */
311
        ( signed char * ) "RegTest2",   /* pcName                       */
312
        mainREG_TEST_STACK_SIZE_WORDS,  /* usStackDepth         */
313
        ( void * ) NULL,                                /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
314
        tskIDLE_PRIORITY,                               /* uxPriority           */
315
        xRegTest2Stack,                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */
316
        {                                                               /* xRegions - this task does not use any non-stack data hence all members are zero. */
317
                /* Base address         Length          Parameters */
318
        { 0x00,                         0x00,                   0x00 },
319
        { 0x00,                         0x00,                   0x00 },
320
        { 0x00,                         0x00,                   0x00 }
321
        }
322
};
323
 
324
/*-----------------------------------------------------------*/
325
 
326
int main( void )
327
{
328
        prvSetupHardware();
329
 
330
        /* Create the queue used to pass "I'm alive" messages to the check task. */
331
        xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
332
 
333
        /* One check task uses the task parameter to receive the queue handle.
334
        This allows the file scope variable to be accessed from within the task.
335
        The pvParameters member of xRegTest2Parameters can only be set after the
336
        queue has been created so is set here. */
337
        xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
338
 
339
        /* Create the three test tasks.  Handles to the created tasks are not
340
        required, hence the second parameter is NULL. */
341
        xTaskCreateRestricted( &xRegTest1Parameters, NULL );
342
    xTaskCreateRestricted( &xRegTest2Parameters, NULL );
343
        xTaskCreateRestricted( &xCheckTaskParameters, NULL );
344
 
345
        /* Create the tasks that are created using the original xTaskCreate() API
346
        function. */
347
        xTaskCreate(    prvOldStyleUserModeTask,        /* The function that implements the task. */
348
                                        ( signed char * ) "Task1",      /* Text name for the task. */
349
                                        100,                                            /* Stack depth in words. */
350
                                        NULL,                                           /* Task parameters. */
351
                                        3,                                                      /* Priority and mode (user in this case). */
352
                                        NULL                                            /* Handle. */
353
                                );
354
 
355
        xTaskCreate(    prvOldStylePrivilegedModeTask,  /* The function that implements the task. */
356
                                        ( signed char * ) "Task2",              /* Text name for the task. */
357
                                        100,                                                    /* Stack depth in words. */
358
                                        NULL,                                                   /* Task parameters. */
359
                                        ( 3 | portPRIVILEGE_BIT ),              /* Priority and mode. */
360
                                        NULL                                                    /* Handle. */
361
                                );
362
 
363
        /* Start the scheduler. */
364
        vTaskStartScheduler();
365
 
366
        /* Will only get here if there was insufficient memory to create the idle
367
        task. */
368
        for( ;; );
369
        return 0;
370
}
371
/*-----------------------------------------------------------*/
372
 
373
static void prvCheckTask( void *pvParameters )
374
{
375
/* This task is created in privileged mode so can access the file scope
376
queue variable.  Take a stack copy of this before the task is set into user
377
mode.  Once that task is in user mode the file scope queue variable will no
378
longer be accessible but the stack copy will. */
379
xQueueHandle xQueue = xFileScopeCheckQueue;
380
long lMessage;
381
unsigned long ulStillAliveCounts[ 2 ] = { 0 };
382
char *pcStatusMessage = "PASS\r\n";
383
unsigned char x = 5, y = 10;
384
 
385
        /* Just to remove compiler warning. */
386
        ( void ) pvParameters;
387
 
388
        /* Demonstrate how the various memory regions can and can't be accessed.
389
        The task privilege is set down to user mode within this function. */
390
        prvTestMemoryRegions();
391
 
392
        /* Change the memory regions allocated to this task to those initially
393
        set up for demonstration purposes to those actually required by the task. */
394
        vTaskAllocateMPURegions( NULL, xAltRegions );
395
 
396
        /* This loop performs the main function of the task, which is blocking
397
        on a message queue then processing each message as it arrives. */
398
        for( ;; )
399
        {
400
                /* Wait for the next message to arrive. */
401
                xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
402
 
403
                switch( lMessage )
404
                {
405
                        case mainREG_TEST_1_STILL_EXECUTING     :
406
                                        /* Message from task 1, so task 1 must still be executing. */
407
                                        ( ulStillAliveCounts[ 0 ] )++;
408
                                        break;
409
 
410
                        case mainREG_TEST_2_STILL_EXECUTING     :
411
                                        /* Message from task 2, so task 2 must still be executing. */
412
                                        ( ulStillAliveCounts[ 1 ] )++;
413
                                        break;
414
 
415
                        case mainPRINT_SYSTEM_STATUS            :
416
                                        /* Message from tick hook, time to print out the system
417
                                        status.  If messages has stopped arriving from either reg
418
                                        test task then the status must be set to fail. */
419
                                        if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 )  )
420
                                        {
421
                                                /* One or both of the test tasks are no longer sending
422
                                                'still alive' messages. */
423
                                                pcStatusMessage = "FAIL\r\n";
424
                                        }
425
 
426
                                        /* Print a pass/fail message to the LCD - moving the
427
                                        message each time to provide feedback that the output
428
                                        is still being produced.  LCD_PrintString() accesses const
429
                                        data stored in flash, which all tasks are at liberty to do,
430
                                        and GPIO for which an MPU region has been set up for it. */
431
                                        LCD_ClearScreen();
432
                                        LCD_PrintString( x>>1, y>>1, pcStatusMessage, 6, COLOR_RED );
433
                                        x += 7;
434
                                        y += 9;
435
 
436
                                        /* Reset the count of 'still alive' messages. */
437
                                        memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
438
                                        break;
439
 
440
                default :
441
                                        /* Something unexpected happened.  Delete this task so the
442
                                        error is apparent (no output will be displayed). */
443
                                        prvDeleteMe();
444
                                        break;
445
                }
446
        }
447
}
448
/*-----------------------------------------------------------*/
449
 
450
static void prvTestMemoryRegions( void )
451
{
452
long l;
453
char cTemp;
454
 
455
        /* The check task (from which this function is called) is created in the
456
        Privileged mode.  The privileged array can be both read from and written
457
        to while this task is privileged. */
458
        cPrivilegedOnlyAccessArray[ 0 ] = 'a';
459
        if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
460
        {
461
                /* Something unexpected happened.  Delete this task so the error is
462
                apparent (no output will be displayed). */
463
                prvDeleteMe();
464
        }
465
 
466
        /* Writing off the end of the RAM allocated to this task will *NOT* cause a
467
        protection fault because the task is still executing in a privileged mode.
468
        Uncomment the following to test. */
469
        /* cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a'; */
470
 
471
        /* Now set the task into user mode. */
472
        portSWITCH_TO_USER_MODE();
473
 
474
        /* Accessing the privileged only array will now cause a fault.  Uncomment
475
        the following line to test. */
476
        /* cPrivilegedOnlyAccessArray[ 0 ] = 'a'; */
477
 
478
        /* The read/write array can still be successfully read and written. */
479
        for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
480
        {
481
                cReadWriteArray[ l ] = 'a';
482
                if( cReadWriteArray[ l ] != 'a' )
483
                {
484
                        /* Something unexpected happened.  Delete this task so the error is
485
                        apparent (no output will be displayed). */
486
                        prvDeleteMe();
487
                }
488
        }
489
 
490
        /* But attempting to read or write off the end of the RAM allocated to this
491
        task will cause a fault.  Uncomment either of the following two lines to
492
        test. */
493
        /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
494
        /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
495
 
496
        /* The read only array can be successfully read... */
497
        for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
498
        {
499
                cTemp = cReadOnlyArray[ l ];
500
        }
501
 
502
        /* ...but cannot be written.  Uncomment the following line to test. */
503
        /* cReadOnlyArray[ 0 ] = 'a'; */
504
 
505
        /* Writing to the first and last locations in the stack array should not
506
        cause a protection fault.  Note that doing this will cause the kernel to
507
        detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
508
        1. */
509
    xCheckTaskStack[ 0 ] = 0;
510
    xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
511
 
512
        /* Writing off either end of the stack array should cause a protection
513
        fault, uncomment either of the following two lines to test. */
514
        /* xCheckTaskStack[ -1 ] = 0; */
515
    /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
516
}
517
/*-----------------------------------------------------------*/
518
 
519
static void prvRegTest1Task( void *pvParameters )
520
{
521
/* This task is created in privileged mode so can access the file scope
522
queue variable.  Take a stack copy of this before the task is set into user
523
mode.  Once this task is in user mode the file scope queue variable will no
524
longer be accessible but the stack copy will. */
525
xQueueHandle xQueue = xFileScopeCheckQueue;
526
 
527
        /* Now the queue handle has been obtained the task can switch to user
528
        mode.  This is just one method of passing a handle into a protected
529
        task, the other reg test task uses the task parameter instead. */
530
    portSWITCH_TO_USER_MODE();
531
 
532
        /* First check that the parameter value is as expected. */
533
        if( pvParameters != ( void * ) 0x12345678 )
534
        {
535
                /* Error detected.  Delete the task so it stops communicating with
536
                the check task. */
537
                prvDeleteMe();
538
        }
539
 
540
 
541
        for( ;; )
542
        {
543
                /* This task tests the kernel context switch mechanism by reading and
544
                writing directly to registers - which requires the test to be written
545
                in assembly code. */
546
                __asm volatile
547
                (
548
                        "               MOV     R4, #104                        \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */
549
                        "               MOV     R5, #105                        \n"
550
                        "               MOV     R6, #106                        \n"
551
                        "               MOV     R8, #108                        \n"
552
                        "               MOV     R9, #109                        \n"
553
                        "               MOV     R10, #110                       \n"
554
                        "               MOV     R11, #111                       \n"
555
                        "reg1loop:                                              \n"
556
                        "               MOV     R0, #100                        \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
557
                        "               MOV     R1, #101                        \n"
558
                        "               MOV     R2, #102                        \n"
559
                        "               MOV R3, #103                    \n"
560
                        "               MOV     R12, #112                       \n"
561
                        "               SVC #1                                  \n" /* Yield just to increase test coverage. */
562
                        "               CMP     R0, #100                        \n" /* Check all the registers still contain their expected values. */
563
                        "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
564
                        "               CMP     R1, #101                        \n"
565
                        "               BNE     prvDeleteMe                     \n"
566
                        "               CMP     R2, #102                        \n"
567
                        "               BNE     prvDeleteMe                     \n"
568
                        "               CMP R3, #103                    \n"
569
                        "               BNE     prvDeleteMe                     \n"
570
                        "               CMP     R4, #104                        \n"
571
                        "               BNE     prvDeleteMe                     \n"
572
                        "               CMP     R5, #105                        \n"
573
                        "               BNE     prvDeleteMe                     \n"
574
                        "               CMP     R6, #106                        \n"
575
                        "               BNE     prvDeleteMe                     \n"
576
                        "               CMP     R8, #108                        \n"
577
                        "               BNE     prvDeleteMe                     \n"
578
                        "               CMP     R9, #109                        \n"
579
                        "               BNE     prvDeleteMe                     \n"
580
                        "               CMP     R10, #110                       \n"
581
                        "               BNE     prvDeleteMe                     \n"
582
                        "               CMP     R11, #111                       \n"
583
                        "               BNE     prvDeleteMe                     \n"
584
                        "               CMP     R12, #112                       \n"
585
                        "               BNE     prvDeleteMe                     \n"
586
                        :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
587
                );
588
 
589
                /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
590
                task is still functioning. */
591
                prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
592
 
593
                /* Go back to check all the register values again. */
594
                __asm volatile( "               B reg1loop      " );
595
        }
596
}
597
/*-----------------------------------------------------------*/
598
 
599
static void prvRegTest2Task( void *pvParameters )
600
{
601
/* The queue handle is passed in as the task parameter.  This is one method of
602
passing data into a protected task, the other reg test task uses a different
603
method. */
604
xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
605
 
606
        for( ;; )
607
        {
608
                /* This task tests the kernel context switch mechanism by reading and
609
                writing directly to registers - which requires the test to be written
610
                in assembly code. */
611
                __asm volatile
612
                (
613
                        "               MOV     R4, #4                          \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */
614
                        "               MOV     R5, #5                          \n"
615
                        "               MOV     R6, #6                          \n"
616
                        "               MOV     R8, #8                          \n" /* Frame pointer is omitted as it must not be changed. */
617
                        "               MOV     R9, #9                          \n"
618
                        "               MOV     R10, 10                         \n"
619
                        "               MOV     R11, #11                        \n"
620
                        "reg2loop:                                              \n"
621
                        "               MOV     R0, #13                         \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
622
                        "               MOV     R1, #1                          \n"
623
                        "               MOV     R2, #2                          \n"
624
                        "               MOV R3, #3                              \n"
625
                        "               MOV     R12, #12                        \n"
626
                        "               CMP     R0, #13                         \n" /* Check all the registers still contain their expected values. */
627
                        "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
628
                        "               CMP     R1, #1                          \n"
629
                        "               BNE     prvDeleteMe                     \n"
630
                        "               CMP     R2, #2                          \n"
631
                        "               BNE     prvDeleteMe                     \n"
632
                        "               CMP R3, #3                              \n"
633
                        "               BNE     prvDeleteMe                     \n"
634
                        "               CMP     R4, #4                          \n"
635
                        "               BNE     prvDeleteMe                     \n"
636
                        "               CMP     R5, #5                          \n"
637
                        "               BNE     prvDeleteMe                     \n"
638
                        "               CMP     R6, #6                          \n"
639
                        "               BNE     prvDeleteMe                     \n"
640
                        "               CMP     R8, #8                          \n"
641
                        "               BNE     prvDeleteMe                     \n"
642
                        "               CMP     R9, #9                          \n"
643
                        "               BNE     prvDeleteMe                     \n"
644
                        "               CMP     R10, #10                        \n"
645
                        "               BNE     prvDeleteMe                     \n"
646
                        "               CMP     R11, #11                        \n"
647
                        "               BNE     prvDeleteMe                     \n"
648
                        "               CMP     R12, #12                        \n"
649
                        "               BNE     prvDeleteMe                     \n"
650
            :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
651
                );
652
 
653
                /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
654
                task is still functioning. */
655
                prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
656
 
657
                /* Go back to check all the register values again. */
658
                __asm volatile( "               B reg2loop      " );
659
        }
660
}
661
/*-----------------------------------------------------------*/
662
 
663
void vApplicationIdleHook( void )
664
{
665
extern unsigned long __SRAM_segment_end__[];
666
extern unsigned long __privileged_data_start__[];
667
extern unsigned long __privileged_data_end__[];
668
extern unsigned long __FLASH_segment_start__[];
669
extern unsigned long __FLASH_segment_end__[];
670
volatile unsigned long *pul;
671
volatile unsigned long ulReadData;
672
 
673
        /* The idle task, and therefore this function, run in Supervisor mode and
674
        can therefore access all memory.  Try reading from corners of flash and
675
        RAM to ensure a memory fault does not occur.
676
 
677
        Start with the edges of the privileged data area. */
678
        pul = __privileged_data_start__;
679
        ulReadData = *pul;
680
        pul = __privileged_data_end__ - 1;
681
        ulReadData = *pul;
682
 
683
        /* Next the standard SRAM area. */
684
        pul = __SRAM_segment_end__ - 1;
685
        ulReadData = *pul;
686
 
687
        /* And the standard Flash area - the start of which is marked for
688
        privileged access only. */
689
        pul = __FLASH_segment_start__;
690
        ulReadData = *pul;
691
        pul = __FLASH_segment_end__ - 1;
692
        ulReadData = *pul;
693
 
694
        /* Reading off the end of Flash or SRAM space should cause a fault.
695
        Uncomment one of the following two pairs of lines to test. */
696
 
697
        /* pul = __FLASH_segment_end__ + 4;
698
        ulReadData = *pul; */
699
 
700
        /* pul = __SRAM_segment_end__ + 1;
701
        ulReadData = *pul; */
702
}
703
/*-----------------------------------------------------------*/
704
 
705
static void prvOldStyleUserModeTask( void *pvParameters )
706
{
707
extern unsigned long __privileged_data_start__[];
708
extern unsigned long __privileged_data_end__[];
709
extern unsigned long __SRAM_segment_end__[];
710
extern unsigned long __privileged_functions_end__[];
711
extern unsigned long __FLASH_segment_start__[];
712
extern unsigned long __FLASH_segment_end__[];
713
const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
714
volatile unsigned long *pul;
715
volatile unsigned long ulReadData;
716
 
717
/* The following lines are commented out to prevent the unused variable
718
compiler warnings when the tests that use the variable are also commented out.
719
extern unsigned long __privileged_functions_start__[];
720
const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
721
 
722
        ( void ) pvParameters;
723
 
724
        /* This task is created in User mode using the original xTaskCreate() API
725
        function.  It should have access to all Flash and RAM except that marked
726
        as Privileged access only.  Reading from the start and end of the non-
727
        privileged RAM should not cause a problem (the privileged RAM is the first
728
        block at the bottom of the RAM memory). */
729
        pul = __privileged_data_end__ + 1;
730
        ulReadData = *pul;
731
        pul = __SRAM_segment_end__ - 1;
732
        ulReadData = *pul;
733
 
734
        /* Likewise reading from the start and end of the non-privileged Flash
735
        should not be a problem (the privileged Flash is the first block at the
736
        bottom of the Flash memory). */
737
        pul = __privileged_functions_end__ + 1;
738
        ulReadData = *pul;
739
        pul = __FLASH_segment_end__ - 1;
740
        ulReadData = *pul;
741
 
742
        /* Standard peripherals are accessible. */
743
        ulReadData = *pulStandardPeripheralRegister;
744
 
745
        /* System peripherals are not accessible.  Uncomment the following line
746
        to test.  Also uncomment the declaration of pulSystemPeripheralRegister
747
        at the top of this function. */
748
    /* ulReadData = *pulSystemPeripheralRegister; */
749
 
750
        /* Reading from anywhere inside the privileged Flash or RAM should cause a
751
        fault.  This can be tested by uncommenting any of the following pairs of
752
        lines.  Also uncomment the declaration of __privileged_functions_start__
753
        at the top of this function. */
754
 
755
        /* pul = __privileged_functions_start__;
756
        ulReadData = *pul; */
757
 
758
        /* pul = __privileged_functions_end__ - 1;
759
        ulReadData = *pul; */
760
 
761
        /* pul = __privileged_data_start__;
762
        ulReadData = *pul; */
763
 
764
        /* pul = __privileged_data_end__ - 1;
765
        ulReadData = *pul; */
766
 
767
        /* Must not just run off the end of a task function, so delete this task.
768
        Note that because this task was created using xTaskCreate() the stack was
769
        allocated dynamically and I have not included any code to free it again. */
770
        vTaskDelete( NULL );
771
}
772
/*-----------------------------------------------------------*/
773
 
774
static void prvOldStylePrivilegedModeTask( void *pvParameters )
775
{
776
extern unsigned long __privileged_data_start__[];
777
extern unsigned long __privileged_data_end__[];
778
extern unsigned long __SRAM_segment_end__[];
779
extern unsigned long __privileged_functions_start__[];
780
extern unsigned long __privileged_functions_end__[];
781
extern unsigned long __FLASH_segment_start__[];
782
extern unsigned long __FLASH_segment_end__[];
783
volatile unsigned long *pul;
784
volatile unsigned long ulReadData;
785
const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
786
const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
787
 
788
        ( void ) pvParameters;
789
 
790
        /* This task is created in Privileged mode using the original xTaskCreate()
791
        API     function.  It should have access to all Flash and RAM including that
792
        marked as Privileged access only.  So reading from the start and end of the
793
        non-privileged RAM should not cause a problem (the privileged RAM is the
794
        first block at the bottom of the RAM memory). */
795
        pul = __privileged_data_end__ + 1;
796
        ulReadData = *pul;
797
        pul = __SRAM_segment_end__ - 1;
798
        ulReadData = *pul;
799
 
800
        /* Likewise reading from the start and end of the non-privileged Flash
801
        should not be a problem (the privileged Flash is the first block at the
802
        bottom of the Flash memory). */
803
        pul = __privileged_functions_end__ + 1;
804
        ulReadData = *pul;
805
        pul = __FLASH_segment_end__ - 1;
806
        ulReadData = *pul;
807
 
808
        /* Reading from anywhere inside the privileged Flash or RAM should also
809
        not be a problem. */
810
        pul = __privileged_functions_start__;
811
        ulReadData = *pul;
812
        pul = __privileged_functions_end__ - 1;
813
        ulReadData = *pul;
814
        pul = __privileged_data_start__;
815
        ulReadData = *pul;
816
        pul = __privileged_data_end__ - 1;
817
        ulReadData = *pul;
818
 
819
        /* Finally, accessing both System and normal peripherals should both be
820
        possible. */
821
    ulReadData = *pulSystemPeripheralRegister;
822
        ulReadData = *pulStandardPeripheralRegister;
823
 
824
        /* Must not just run off the end of a task function, so delete this task.
825
        Note that because this task was created using xTaskCreate() the stack was
826
        allocated dynamically and I have not included any code to free it again. */
827
        vTaskDelete( NULL );
828
}
829
/*-----------------------------------------------------------*/
830
 
831
static void prvDeleteMe( void )
832
{
833
        vTaskDelete( NULL );
834
}
835
/*-----------------------------------------------------------*/
836
 
837
static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
838
{
839
        if( xHandle != NULL )
840
        {
841
                xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
842
        }
843
}
844
/*-----------------------------------------------------------*/
845
 
846
void prvSetupHardware( void )
847
{
848
        /* Disable peripherals power. */
849
        SC->PCONP = 0;
850
 
851
        /* Enable GPIO power. */
852
        SC->PCONP = PCONP_PCGPIO;
853
 
854
        /* Disable TPIU. */
855
        PINCON->PINSEL10 = 0;
856
 
857
        if ( SC->PLL0STAT & ( 1 << 25 ) )
858
        {
859
                /* Enable PLL, disconnected. */
860
                SC->PLL0CON = 1;
861
                SC->PLL0FEED = PLLFEED_FEED1;
862
                SC->PLL0FEED = PLLFEED_FEED2;
863
        }
864
 
865
        /* Disable PLL, disconnected. */
866
        SC->PLL0CON = 0;
867
        SC->PLL0FEED = PLLFEED_FEED1;
868
        SC->PLL0FEED = PLLFEED_FEED2;
869
 
870
        /* Enable main OSC. */
871
        SC->SCS |= 0x20;
872
        while( !( SC->SCS & 0x40 ) );
873
 
874
        /* select main OSC, 12MHz, as the PLL clock source. */
875
        SC->CLKSRCSEL = 0x1;
876
 
877
        SC->PLL0CFG = 0x20031;
878
        SC->PLL0FEED = PLLFEED_FEED1;
879
        SC->PLL0FEED = PLLFEED_FEED2;
880
 
881
        /* Enable PLL, disconnected. */
882
        SC->PLL0CON = 1;
883
        SC->PLL0FEED = PLLFEED_FEED1;
884
        SC->PLL0FEED = PLLFEED_FEED2;
885
 
886
        /* Set clock divider. */
887
        SC->CCLKCFG = 0x03;
888
 
889
        /* Configure flash accelerator. */
890
        SC->FLASHCFG = 0x403a;
891
 
892
        /* Check lock bit status. */
893
        while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
894
 
895
        /* Enable and connect. */
896
        SC->PLL0CON = 3;
897
        SC->PLL0FEED = PLLFEED_FEED1;
898
        SC->PLL0FEED = PLLFEED_FEED2;
899
        while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
900
 
901
 
902
 
903
 
904
        /* Configure the clock for the USB. */
905
 
906
        if( SC->PLL1STAT & ( 1 << 9 ) )
907
        {
908
                /* Enable PLL, disconnected. */
909
                SC->PLL1CON = 1;
910
                SC->PLL1FEED = PLLFEED_FEED1;
911
                SC->PLL1FEED = PLLFEED_FEED2;
912
        }
913
 
914
        /* Disable PLL, disconnected. */
915
        SC->PLL1CON = 0;
916
        SC->PLL1FEED = PLLFEED_FEED1;
917
        SC->PLL1FEED = PLLFEED_FEED2;
918
 
919
        SC->PLL1CFG = 0x23;
920
        SC->PLL1FEED = PLLFEED_FEED1;
921
        SC->PLL1FEED = PLLFEED_FEED2;
922
 
923
        /* Enable PLL, disconnected. */
924
        SC->PLL1CON = 1;
925
        SC->PLL1FEED = PLLFEED_FEED1;
926
        SC->PLL1FEED = PLLFEED_FEED2;
927
        while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
928
 
929
        /* Enable and connect. */
930
        SC->PLL1CON = 3;
931
        SC->PLL1FEED = PLLFEED_FEED1;
932
        SC->PLL1FEED = PLLFEED_FEED2;
933
        while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
934
 
935
        /*  Setup the peripheral bus to be the same as the PLL output (64 MHz). */
936
        SC->PCLKSEL0 = 0x05555555;
937
 
938
        /* Prepare the LCD. */
939
        LCDdriver_initialisation();
940
        LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);
941
}
942
/*-----------------------------------------------------------*/
943
 
944
void vApplicationTickHook( void )
945
{
946
static unsigned long ulCallCount;
947
const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
948
const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
949
portBASE_TYPE xDummy;
950
 
951
        /* If configUSE_TICK_HOOK is set to 1 then this function will get called
952
        from each RTOS tick.  It is called from the tick interrupt and therefore
953
        will be executing in the privileged state. */
954
 
955
        ulCallCount++;
956
 
957
        /* Is it time to print out the pass/fail message again? */
958
        if( ulCallCount >= ulCallsBetweenSends )
959
        {
960
                ulCallCount = 0;
961
 
962
                /* Send a message to the check task to command it to check that all
963
                the tasks are still running then print out the status.
964
 
965
                This is running in an ISR so has to use the "FromISR" version of
966
                xQueueSend().  Because it is in an ISR it is running with privileges
967
                so can access xFileScopeCheckQueue directly. */
968
                xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
969
        }
970
}
971
/*-----------------------------------------------------------*/
972
 
973
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
974
{
975
        /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
976
        function will automatically get called if a task overflows its stack. */
977
        ( void ) pxTask;
978
        ( void ) pcTaskName;
979
        for( ;; );
980
}
981
/*-----------------------------------------------------------*/
982
 
983
void vApplicationMallocFailedHook( void )
984
{
985
        /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
986
        be called automatically if a call to pvPortMalloc() fails.  pvPortMalloc()
987
        is called automatically when a task, queue or semaphore is created. */
988
        for( ;; );
989
}
990
/*-----------------------------------------------------------*/
991
 
992
 

powered by: WebSVN 2.1.0

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