OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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

Line No. Rev Author Line
1 583 jeremybenn
/*
2
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
3
 
4
    ***************************************************************************
5
    *                                                                         *
6
    * If you are:                                                             *
7
    *                                                                         *
8
    *    + New to FreeRTOS,                                                   *
9
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
10
    *    + Looking for basic training,                                        *
11
    *    + Wanting to improve your FreeRTOS skills and productivity           *
12
    *                                                                         *
13
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
14
    *                                                                         *
15
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
16
    *                  http://www.FreeRTOS.org/Documentation                  *
17
    *                                                                         *
18
    * A pdf reference manual is also available.  Both are usually delivered   *
19
    * to your inbox within 20 minutes to two hours when purchased between 8am *
20
    * and 8pm GMT (although please allow up to 24 hours in case of            *
21
    * exceptional circumstances).  Thank you for your support!                *
22
    *                                                                         *
23
    ***************************************************************************
24
 
25
    This file is part of the FreeRTOS distribution.
26
 
27
    FreeRTOS is free software; you can redistribute it and/or modify it under
28
    the terms of the GNU General Public License (version 2) as published by the
29
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30
    ***NOTE*** The exception to the GPL is included to allow you to distribute
31
    a combined work that includes FreeRTOS without being obliged to provide the
32
    source code for proprietary components outside of the FreeRTOS kernel.
33
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
36
    more details. You should have received a copy of the GNU General Public
37
    License and the FreeRTOS license exception along with FreeRTOS; if not it
38
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
39
    by writing to Richard Barry, contact details for whom are available on the
40
    FreeRTOS WEB site.
41
 
42
    1 tab == 4 spaces!
43
 
44
    http://www.FreeRTOS.org - Documentation, latest information, license and
45
    contact details.
46
 
47
    http://www.SafeRTOS.com - A version that is certified for use in safety
48
    critical systems.
49
 
50
    http://www.OpenRTOS.com - Commercial support, development, porting,
51
    licensing and training services.
52
*/
53
 
54
/*
55
 * Creates the demo application tasks, then starts the scheduler.  The WEB
56
 * documentation provides more details of the demo application tasks.
57
 *
58
 * Main. c also creates four other tasks:
59
 *
60
 * 1) vErrorChecks()
61
 * This only executes every few seconds but has the highest priority so is
62
 * guaranteed to get processor time.  Its main function is to check that all
63
 * the standard demo application tasks are still operational and have not
64
 * experienced any errors.  vErrorChecks() will toggle the on board LED
65
 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
66
 * tasks have reported an error.  Should any task report an error at any time
67
 * the rate at which the on board LED is toggled is increased to
68
 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
69
 * wrong.
70
 *
71
 * 2) vRegisterCheck()
72
 * This is a very simple task that checks that all the registers are always
73
 * in their expected state.  The task only makes use of the A register, so
74
 * all the other registers should always contain their initial values.
75
 * An incorrect value indicates an error in the context switch mechanism.
76
 * The task operates at the idle priority so will be preempted regularly.
77
 * Any error will cause the toggle rate of the on board LED to increase to
78
 * mainERROR_FLASH_PERIOD milliseconds.
79
 *
80
 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
81
 * These are very basic versions of the standard FLOP tasks.  They are good
82
 * at detecting errors in the context switch mechanism, and also check that
83
 * the floating point libraries are correctly built to be re-enterant.  The
84
 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
85
 * tasks.
86
 */
87
 
88
/* Standard includes. */
89
#include <stdlib.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
 
103
/* Demo task priorities. */
104
#define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
105
#define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
106
#define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
107
#define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
108
#define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
109
#define mainINTEGER_PRIORITY            tskIDLE_PRIORITY
110
 
111
/* Constants required to disable the watchdog. */
112
#define mainDISABLE_BYTE_1                      ( ( unsigned char ) 0xde )
113
#define mainDISABLE_BYTE_2                      ( ( unsigned char ) 0xad )
114
 
115
/* Constants to setup and use the on board LED. */
116
#define ucLED_BIT                                       ( ( unsigned char ) 0x40 )
117
#define mainPORT_1_BIT_6                        ( ( unsigned char ) 0x40 )
118
#define mainENABLE_CROSS_BAR            ( ( unsigned char ) 0x40 )
119
 
120
/* Constants to set the clock frequency. */
121
#define mainSELECT_INTERNAL_OSC         ( ( unsigned char ) 0x80 )
122
#define mainDIVIDE_CLOCK_BY_1           ( ( unsigned char ) 0x03 )
123
#define mainPLL_USES_INTERNAL_OSC       ( ( unsigned char ) 0x04 )
124
#define mainFLASH_READ_TIMING           ( ( unsigned char ) 0x30 )
125
#define mainPLL_POWER_ON                        ( ( unsigned char ) 0x01 )
126
#define mainPLL_NO_PREDIVIDE            ( ( unsigned char ) 0x01 )
127
#define mainPLL_FILTER                          ( ( unsigned char ) 0x01 )
128
#define mainPLL_MULTIPLICATION          ( ( unsigned char ) 0x04 )
129
#define mainENABLE_PLL                          ( ( unsigned char ) 0x02 )
130
#define mainPLL_LOCKED                          ( ( unsigned char ) 0x10 )
131
#define mainSELECT_PLL_AS_SOURCE        ( ( unsigned char ) 0x02 )
132
 
133
/* Toggle rate for the on board LED - which is dependent on whether or not
134
an error has been detected. */
135
#define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 5000 )
136
#define mainERROR_FLASH_PERIOD          ( ( portTickType ) 250 )
137
 
138
/* Baud rate used by the serial port tasks. */
139
#define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )
140
 
141
/* Pass an invalid LED number to the COM test task as we don't want it to flash
142
an LED.  There are only 8 LEDs (excluding the on board LED) wired in and these
143
are all used by the flash tasks. */
144
#define mainCOM_TEST_LED                        ( 200 )
145
 
146
/* We want the Cygnal to act as much as possible as a standard 8052. */
147
#define mainAUTO_SFR_OFF                        ( ( unsigned char ) 0 )
148
 
149
/* Constants required to setup the IO pins for serial comms. */
150
#define mainENABLE_COMS                         ( ( unsigned char ) 0x04 )
151
#define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
152
 
153
/* Pointer passed as a parameter to vRegisterCheck() just so it has some know
154
values to check for in the DPH, DPL and B registers. */
155
#define mainDUMMY_POINTER               ( ( xdata void * ) 0xabcd )
156
 
157
/* Macro that lets vErrorChecks() know that one of the tasks defined in
158
main. c has detected an error.  A critical region is used around xLatchError
159
as it is accessed from vErrorChecks(), which has a higher priority. */
160
#define mainLATCH_ERROR()                       \
161
{                                                                       \
162
        portENTER_CRITICAL();                   \
163
                xLatchedError = pdTRUE;         \
164
        portEXIT_CRITICAL();                    \
165
}
166
 
167
/*
168
 * Setup the Cygnal microcontroller for its fastest operation.
169
 */
170
static void prvSetupSystemClock( void );
171
 
172
/*
173
 * Setup the peripherals, including the on board LED.
174
 */
175
static void prvSetupHardware( void );
176
 
177
/*
178
 * Toggle the state of the on board LED.
179
 */
180
static void prvToggleOnBoardLED( void );
181
 
182
/*
183
 * See comments at the top of the file for details.
184
 */
185
static void vErrorChecks( void *pvParameters );
186
 
187
/*
188
 * See comments at the top of the file for details.
189
 */
190
static void vRegisterCheck( void *pvParameters );
191
 
192
/*
193
 * See comments at the top of the file for details.
194
 */
195
static void vFLOPCheck1( void *pvParameters );
196
 
197
/*
198
 * See comments at the top of the file for details.
199
 */
200
static void vFLOPCheck2( void *pvParameters );
201
 
202
/* File scope variable used to communicate the occurrence of an error between
203
tasks. */
204
static portBASE_TYPE xLatchedError = pdFALSE;
205
 
206
/*-----------------------------------------------------------*/
207
 
208
/*
209
 * Starts all the other tasks, then starts the scheduler.
210
 */
211
void main( void )
212
{
213
        /* Initialise the hardware including the system clock and on board
214
        LED. */
215
        prvSetupHardware();
216
 
217
        /* Initialise the port that controls the external LED's utilized by the
218
        flash tasks. */
219
        vParTestInitialise();
220
 
221
        /* Start the used standard demo tasks. */
222
        vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
223
        vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
224
        vStartIntegerMathTasks( mainINTEGER_PRIORITY );
225
        vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
226
        vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
227
 
228
        /* Start the tasks defined in this file.  The first three never block so
229
        must not be used with the co-operative scheduler. */
230
        #if configUSE_PREEMPTION == 1
231
        {
232
                xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
233
                xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
234
                xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
235
        }
236
        #endif 
237
 
238
        xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( xTaskHandle * ) NULL );
239
 
240
        /* Finally kick off the scheduler.  This function should never return. */
241
        vTaskStartScheduler();
242
 
243
        /* Should never reach here as the tasks will now be executing under control
244
        of the scheduler. */
245
}
246
/*-----------------------------------------------------------*/
247
 
248
/*
249
 * Setup the hardware prior to using the scheduler.  Most of the Cygnal
250
 * specific initialisation is performed here leaving standard 8052 setup
251
 * only in the driver code.
252
 */
253
static void prvSetupHardware( void )
254
{
255
unsigned char ucOriginalSFRPage;
256
 
257
        /* Remember the SFR page before it is changed so it can get set back
258
        before the function exits. */
259
        ucOriginalSFRPage = SFRPAGE;
260
 
261
        /* Setup the SFR page to access the config SFR's. */
262
        SFRPAGE = CONFIG_PAGE;
263
 
264
        /* Don't allow the microcontroller to automatically switch SFR page, as the
265
        SFR page is not stored as part of the task context. */
266
        SFRPGCN = mainAUTO_SFR_OFF;
267
 
268
        /* Disable the watchdog. */
269
        WDTCN = mainDISABLE_BYTE_1;
270
        WDTCN = mainDISABLE_BYTE_2;
271
 
272
        /* Set the on board LED to push pull. */
273
        P1MDOUT |= mainPORT_1_BIT_6;
274
 
275
        /* Setup the cross bar to enable serial comms here as it is not part of the
276
        standard 8051 setup and therefore is not in the driver code. */
277
        XBR0 |= mainENABLE_COMS;
278
        P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
279
 
280
        /* Enable the cross bar so our hardware setup takes effect. */
281
        XBR2 = mainENABLE_CROSS_BAR;
282
 
283
        /* Setup a fast system clock. */
284
        prvSetupSystemClock();
285
 
286
        /* Return the SFR page. */
287
        SFRPAGE = ucOriginalSFRPage;
288
}
289
/*-----------------------------------------------------------*/
290
 
291
static void prvSetupSystemClock( void )
292
{
293
volatile unsigned short usWait;
294
const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
295
unsigned char ucOriginalSFRPage;
296
 
297
        /* Remember the SFR page so we can set it back at the end. */
298
        ucOriginalSFRPage = SFRPAGE;
299
        SFRPAGE = CONFIG_PAGE;
300
 
301
        /* Use the internal oscillator set to its fasted frequency. */
302
        OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
303
 
304
        /* Ensure the clock is stable. */
305
        for( usWait = 0; usWait < usWaitTime; usWait++ );
306
 
307
        /* Setup the clock source for the PLL. */
308
        PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
309
 
310
        /* Change the read timing for the flash ready for the fast clock. */
311
        SFRPAGE = LEGACY_PAGE;
312
        FLSCL |= mainFLASH_READ_TIMING;
313
 
314
        /* Turn on the PLL power. */
315
        SFRPAGE = CONFIG_PAGE;
316
        PLL0CN |= mainPLL_POWER_ON;
317
 
318
        /* Don't predivide the clock. */
319
        PLL0DIV = mainPLL_NO_PREDIVIDE;
320
 
321
        /* Set filter for fastest clock. */
322
        PLL0FLT = mainPLL_FILTER;
323
        PLL0MUL = mainPLL_MULTIPLICATION;
324
 
325
        /* Ensure the clock is stable. */
326
        for( usWait = 0; usWait < usWaitTime; usWait++ );
327
 
328
        /* Enable the PLL and wait for it to lock. */
329
        PLL0CN |= mainENABLE_PLL;
330
        for( usWait = 0; usWait < usWaitTime; usWait++ )
331
        {
332
                if( PLL0CN & mainPLL_LOCKED )
333
                {
334
                        break;
335
                }
336
        }
337
 
338
        /* Select the PLL as the clock source. */
339
        CLKSEL |= mainSELECT_PLL_AS_SOURCE;
340
 
341
        /* Return the SFR back to its original value. */
342
        SFRPAGE = ucOriginalSFRPage;
343
}
344
/*-----------------------------------------------------------*/
345
 
346
static void prvToggleOnBoardLED( void )
347
{
348
        /* If the on board LED is on, turn it off and visa versa. */
349
        if( P1 & ucLED_BIT )
350
        {
351
                P1 &= ~ucLED_BIT;
352
        }
353
        else
354
        {
355
                P1 |= ucLED_BIT;
356
        }
357
}
358
/*-----------------------------------------------------------*/
359
 
360
/*
361
 * See the documentation at the top of this file.
362
 */
363
static void vErrorChecks( void *pvParameters )
364
{
365
portBASE_TYPE xErrorHasOccurred = pdFALSE;
366
 
367
        /* Just to prevent compiler warnings. */
368
        ( void ) pvParameters;
369
 
370
        /* Cycle for ever, delaying then checking all the other tasks are still
371
        operating without error.   The delay period depends on whether an error
372
        has ever been detected. */
373
        for( ;; )
374
        {
375
                if( xLatchedError == pdFALSE )
376
                {
377
                        /* No errors have been detected so delay for a longer period.  The
378
                        on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
379
                        vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
380
                }
381
                else
382
                {
383
                        /* We have at some time recognised an error in one of the demo
384
                        application tasks, delay for a shorter period.  The on board LED
385
                        will get toggled every mainERROR_FLASH_PERIOD ms. */
386
                        vTaskDelay( mainERROR_FLASH_PERIOD );
387
                }
388
 
389
 
390
 
391
                /* Check the demo application tasks for errors. */
392
 
393
                if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
394
                {
395
                        xErrorHasOccurred = pdTRUE;
396
                }
397
 
398
                if( xArePollingQueuesStillRunning() != pdTRUE )
399
                {
400
                        xErrorHasOccurred = pdTRUE;
401
                }
402
 
403
                if( xAreComTestTasksStillRunning() != pdTRUE )
404
                {
405
                        xErrorHasOccurred = pdTRUE;
406
                }
407
 
408
                if( xAreSemaphoreTasksStillRunning() != pdTRUE )
409
                {
410
                        xErrorHasOccurred = pdTRUE;
411
                }
412
 
413
                /* If an error has occurred, latch it to cause the LED flash rate to
414
                increase. */
415
                if( xErrorHasOccurred == pdTRUE )
416
                {
417
                        xLatchedError = pdTRUE;
418
                }
419
 
420
                /* Toggle the LED to indicate the completion of a check cycle.  The
421
                frequency of check cycles is dependent on whether or not we have
422
                latched an error. */
423
                prvToggleOnBoardLED();
424
        }
425
}
426
/*-----------------------------------------------------------*/
427
 
428
/*
429
 * See the documentation at the top of this file.  Also see the standard FLOP
430
 * demo task documentation for the rationale of these tasks.
431
 */
432
static void vFLOPCheck1( void *pvParameters )
433
{
434
volatile portFLOAT fVal1, fVal2, fResult;
435
 
436
        ( void ) pvParameters;
437
 
438
        for( ;; )
439
        {
440
                fVal1 = ( portFLOAT ) -1234.5678;
441
                fVal2 = ( portFLOAT ) 2345.6789;
442
 
443
                fResult = fVal1 + fVal2;
444
                if( ( fResult > ( portFLOAT )  1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
445
                {
446
                        mainLATCH_ERROR();
447
                }
448
 
449
                fResult = fVal1 / fVal2;
450
                if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
451
                {
452
                        mainLATCH_ERROR();
453
                }
454
        }
455
}
456
/*-----------------------------------------------------------*/
457
 
458
/*
459
 * See the documentation at the top of this file.
460
 */
461
static void vFLOPCheck2( void *pvParameters )
462
{
463
volatile portFLOAT fVal1, fVal2, fResult;
464
 
465
        ( void ) pvParameters;
466
 
467
        for( ;; )
468
        {
469
                fVal1 = ( portFLOAT ) -12340.5678;
470
                fVal2 = ( portFLOAT ) 23450.6789;
471
 
472
                fResult = fVal1 + fVal2;
473
                if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
474
                {
475
                        mainLATCH_ERROR();
476
                }
477
 
478
                fResult = fVal1 / -fVal2;
479
                if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
480
                {
481
                        mainLATCH_ERROR();
482
                }
483
        }
484
}
485
/*-----------------------------------------------------------*/
486
 
487
/*
488
 * See the documentation at the top of this file.
489
 */
490
static void vRegisterCheck( void *pvParameters )
491
{
492
        ( void ) pvParameters;
493
 
494
        for( ;; )
495
        {
496
                if( SP != configSTACK_START )
497
                {
498
                        mainLATCH_ERROR();
499
                }
500
 
501
                _asm
502
                        MOV ACC, ar0
503
                _endasm;
504
 
505
                if( ACC != 0 )
506
                {
507
                        mainLATCH_ERROR();
508
                }
509
 
510
                _asm
511
                        MOV ACC, ar1
512
                _endasm;
513
 
514
                if( ACC != 1 )
515
                {
516
                        mainLATCH_ERROR();
517
                }
518
                _asm
519
                        MOV ACC, ar2
520
                _endasm;
521
 
522
                if( ACC != 2 )
523
                {
524
                        mainLATCH_ERROR();
525
                }
526
                _asm
527
                        MOV ACC, ar3
528
                _endasm;
529
 
530
                if( ACC != 3 )
531
                {
532
                        mainLATCH_ERROR();
533
                }
534
                _asm
535
                        MOV ACC, ar4
536
                _endasm;
537
 
538
                if( ACC != 4 )
539
                {
540
                        mainLATCH_ERROR();
541
                }
542
                _asm
543
                        MOV ACC, ar5
544
                _endasm;
545
 
546
                if( ACC != 5 )
547
                {
548
                        mainLATCH_ERROR();
549
                }
550
                _asm
551
                        MOV ACC, ar6
552
                _endasm;
553
 
554
                if( ACC != 6 )
555
                {
556
                        mainLATCH_ERROR();
557
                }
558
                _asm
559
                        MOV ACC, ar7
560
                _endasm;
561
 
562
                if( ACC != 7 )
563
                {
564
                        mainLATCH_ERROR();
565
                }
566
 
567
                if( DPL != 0xcd )
568
                {
569
                        mainLATCH_ERROR();
570
                }
571
 
572
                if( DPH != 0xab )
573
                {
574
                        mainLATCH_ERROR();
575
                }
576
 
577
                if( B != 0x01 )
578
                {
579
                        mainLATCH_ERROR();
580
                }
581
        }
582
}
583
 
584
 

powered by: WebSVN 2.1.0

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