⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 This a simple OS for study.
💻 C
字号:
/*
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*
*                           (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
*                                           All Rights Reserved
*
*                                               EXAMPLE #2
*
* Notes: 1) You can run this code on a PC running DOS 4.xx
*        2) This test file has been tested on the JK Microsystems, Inc. uFlashTCP board which contains
*           contains an Intel 80386 CPU running at 25 MHz.  The board doesn't come with a VGA display 
*           controller but you can write to the console port (COM2) using 'printf' statements.  The
*           console keyboard simulates a PC's keyboard.
*********************************************************************************************************
*/

#include "includes.h"

/*
*********************************************************************************************************
*                                               CONSTANTS
*********************************************************************************************************
*/

#define  TEST_TASK_STK_SIZE                 400       /* Size of each task's stacks (# of WORDs)       */

#define  TEST_N_TASKS                        10       /* Number of identical tasks                     */

/*
*********************************************************************************************************
*                                               VARIABLES
*********************************************************************************************************
*/

OS_STK        TestStartStk[TEST_TASK_STK_SIZE];       /* Tasks stacks                                  */

OS_STK        TestTaskStk[TEST_N_TASKS][TEST_TASK_STK_SIZE];

OS_EVENT     *TestRandomSem;
char          TestTaskData[TEST_N_TASKS];             /* Parameters to pass to each task               */

INT16U        TestDly;
INT16U        TestLoops;

/*
*********************************************************************************************************
*                                           FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void  TestStart(void *data);                  /* Function prototypes of Startup task           */
static  void  TestTask(void *data);

static  void  TestCreateTasks(void);
static  void  TestDisp(void);

/*$PAGE*/
/*
*********************************************************************************************************
*                                                MAIN
*********************************************************************************************************
*/

void  main (void)
{
    INT8U  err;
	INT8U  prio;


    OSInit();                                              /* Initialize uC/OS-II                      */

    PC_DOSSaveReturn();                                    /* Save environment to return to DOS        */
    PC_VectSet(uCOS, OSCtxSw);                             /* Install uC/OS-II's context switch vector */

    printf("uC/OS-II and uC/OS-View on 80x86 (Large).\n");

    prio = 0;
    OSTaskCreateExt(TestStart, 
                    (void *)0, 
                    &TestStartStk[TEST_TASK_STK_SIZE - 1], 
                    prio,
                    prio,
                    &TestStartStk[0],
					TEST_TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CLR + OS_TASK_OPT_STK_CHK);
    OSTaskNameSet(prio, "Start Task", &err);

    OSStart();                                             /* Start multitasking                       */
}


/*
*********************************************************************************************************
*                                              STARTUP TASK
*********************************************************************************************************
*/
static  void  TestStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr;
#endif
    char       s[100];
    INT16S     key;
    INT16U     ctr;


    pdata = pdata;                                         /* Prevent compiler warning                 */

    OS_ENTER_CRITICAL();
    PC_VectSet(0x08, OSTickISR);                           /* Install uC/OS-II's clock tick ISR        */
    PC_SetTickRate(OS_TICKS_PER_SEC);                      /* Reprogram tick rate                      */
    OS_EXIT_CRITICAL();

    OSStatInit();                                          /* Initialize uC/OS-II's statistics         */

    OSView_Init();                                         /* Initialize uC/OS-View                    */

    TestCreateTasks();                                     /* Create all the application tasks         */

    PC_DispClrScr(DISP_FGND_WHITE);

    TestDly   = 100;
	TestLoops =  10;
    ctr       =   0;
    while (TRUE) {
        if (PC_GetKey(&key) == TRUE) {                     /* See if key has been pressed              */
            switch (key) {                                 
                case 0x1B:                                 /* See if it's the ESCAPE key               */
			         OSView_Exit();
                     PC_DOSReturn();                       /* Return to DOS                            */
                     break;

                case '+':                                  /* Increase task execution rate             */
                case '=':
                     if (TestDly > 10) {
                         TestDly -= 5;
                     } else if (TestDly > 0) {
                         TestDly--;
					 }
                     break;
                                                           /* Decrease task execution rate             */
                case '-':
                case '_':
                     if (TestDly < 10) {
                         TestDly++;
                     } else if (TestDly <= 95) {
					     TestDly += 5;
					 }
                     break;

                case 'U':                                  /* Increase task execution time             */ 
                case 'u':
                     if (TestLoops <= 990) {
					     TestLoops += 10;
					 }
                     break;

                case 'D':                                  /* Decrease task execution time             */
                case 'd':
                     if (TestLoops > 10) {
                         TestLoops -= 10;
					 }
                     break;
            }
        }

        printf("%05u   #Tasks:%2u   %%CPU:%3u   Dly:%3u   Loops:%3u\n", ctr, OSTaskCtr, OSCPUUsage, TestDly, TestLoops);
        ctr++;
        OSTimeDly(OS_TICKS_PER_SEC);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                             CREATE TASKS
*********************************************************************************************************
*/

static  void  TestCreateTasks (void)
{
    INT8U  err;
	INT8U  prio;
    INT8U  i;
    char   s[20];


    for (i = 0; i < TEST_N_TASKS; i++) {
        sprintf(s, "Task #%2d", i + 1);
        TestTaskData[i] = '0' + i;                         /* Each task will display its own letter    */
        prio            = i + 1;
        OSTaskCreateExt(TestTask, 
                    (void *)&TestTaskData[i], 
                    &TestTaskStk[i][TEST_TASK_STK_SIZE - 1], 
                    prio,
                    prio,
                    &TestTaskStk[i][0],
					TEST_TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CLR + OS_TASK_OPT_STK_CHK);
        OSTaskNameSet(prio, s, &err);
    }
}

/*
*********************************************************************************************************
*                                               TASK #1
*********************************************************************************************************
*/
static  void  TestTask (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr;
#endif
    INT8U      x;
    INT8U      y;
    INT8U      err;
	INT16U     i;

        

    pdata = pdata;                               /* Prevent compiler warning                           */
    while (TRUE) {
        OSSemPend(TestRandomSem, 0, &err);       /* Acquire semaphore to perform random numbers        */
        if (OSTCBCur->OSTCBPrio == 1) {          /* Consume more processing time for task prio. #1     */
		    for (i = 0; i < TestLoops * 5; i++) {
                x = random(80);                  /* Waste time by getting a random number              */
                y = random(10);
		    }
        } else {    
		    for (i = 0; i < TestLoops; i++) {
                x = random(80);                  /* Waste time by getting a random number              */
                y = random(10);
		    }
        }
        OSSemPost(TestRandomSem);                /* Release semaphore                                  */
                                                 /* Display the task number on the screen              */
        OSTimeDly(TestDly);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -