Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.99 KB | None | 0 0
  1. /*
  2. *********************************************************************************************************
  3. *                                                uC/OS-II
  4. *                                          The Real-Time Kernel
  5. *
  6. *                           (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
  7. *                                           All Rights Reserved
  8. *
  9. *                                               EXAMPLE #1
  10. *********************************************************************************************************
  11. */
  12.  
  13. #include "includes.h"
  14.  
  15. /*
  16. *********************************************************************************************************
  17. *                                               CONSTANTS
  18. *********************************************************************************************************
  19. */
  20.  
  21. #define  TASK_STK_SIZE                 512       /* Size of each task's stacks (# of WORDs)            */
  22. #define  N_TASKS                        10       /* Number of identical tasks                          */
  23.  
  24. /*
  25. *********************************************************************************************************
  26. *                                               VARIABLES
  27. *********************************************************************************************************
  28. */
  29.  
  30. OS_STK        TaskStk[N_TASKS][TASK_STK_SIZE];        /* Tasks stacks                                  */
  31. OS_STK        TaskStartStk[TASK_STK_SIZE];
  32. char          TaskData[N_TASKS];                      /* Parameters to pass to each task               */
  33. OS_EVENT     *RandomSem;
  34. char          KeyboardData;
  35. char          DisplayData;
  36.  
  37. /*
  38. *********************************************************************************************************
  39. *                                           FUNCTION PROTOTYPES
  40. *********************************************************************************************************
  41. */
  42.  
  43.         void  Task(void *data);                       /* Function prototypes of tasks                  */
  44.         void  TaskStart(void *data);                  /* Function prototypes of Startup task           */
  45. static  void  TaskStartCreateTasks(void);
  46. static  void  TaskStartDispInit(void);
  47. static  void  TaskStartDisp(void);
  48.  
  49. /*$PAGE*/
  50. /*
  51. *********************************************************************************************************
  52. *                                                MAIN
  53. *********************************************************************************************************
  54. */
  55.  
  56. void  main (void)
  57. {
  58.     PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK);      /* Clear the screen                         */
  59.  
  60.     OSInit();                                              /* Initialize uC/OS-II                      */
  61.  
  62.     PC_DOSSaveReturn();                                    /* Save environment to return to DOS        */
  63.     PC_VectSet(uCOS, OSCtxSw);                             /* Install uC/OS-II's context switch vector */
  64.  
  65.     RandomSem   = OSSemCreate(1);                          /* Random number semaphore                  */
  66.  
  67.     OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);
  68.     OSStart();                                             /* Start multitasking                       */
  69. }
  70.  
  71.  
  72. /*
  73. *********************************************************************************************************
  74. *                                              STARTUP TASK
  75. *********************************************************************************************************
  76. */
  77. void  TaskStart (void *pdata)
  78. {
  79. #if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
  80.     OS_CPU_SR  cpu_sr;
  81. #endif
  82.     char       s[100];
  83.     INT16S     key;
  84.  
  85.  
  86.     pdata = pdata;                                         /* Prevent compiler warning                 */
  87.  
  88.     TaskStartDispInit();                                   /* Initialize the display                   */
  89.  
  90.     OS_ENTER_CRITICAL();
  91.     PC_VectSet(0x08, OSTickISR);                           /* Install uC/OS-II's clock tick ISR        */
  92.     PC_SetTickRate(OS_TICKS_PER_SEC);                      /* Reprogram tick rate                      */
  93.     OS_EXIT_CRITICAL();
  94.  
  95.     OSStatInit();                                          /* Initialize uC/OS-II's statistics         */
  96.  
  97.     TaskStartCreateTasks();                                /* Create all the application tasks         */
  98.  
  99.     for (;;) {
  100.         TaskStartDisp();                                  /* Update the display                       */
  101.  
  102.  
  103.         if (PC_GetKey(&key) == TRUE) {                     /* See if key has been pressed              */
  104.             if (key == 0x1B) {                             /* Yes, see if it's the ESCAPE key          */
  105.                 PC_DOSReturn();                            /* Return to DOS                            */
  106.             }
  107.         }
  108.  
  109.         OSCtxSwCtr = 0;                                    /* Clear context switch counter             */
  110.         OSTimeDlyHMSM(0, 0, 1, 0);                         /* Wait one second                          */
  111.     }
  112. }
  113.  
  114. /*$PAGE*/
  115. /*
  116. *********************************************************************************************************
  117. *                                        INITIALIZE THE DISPLAY
  118. *********************************************************************************************************
  119. */
  120.  
  121. static  void  TaskStartDispInit (void)
  122. {
  123. /*                                1111111111222222222233333333334444444444555555555566666666667777777777 */
  124. /*                      01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
  125.     PC_DispStr( 0,  0, "                         uC/OS-II, The Real-Time Kernel                         ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
  126.     PC_DispStr( 0,  1, "                                Jean J. Labrosse                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  127.     PC_DispStr( 0,  2, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  128.     PC_DispStr( 0,  3, "                                    EXAMPLE #1                                  ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  129.     PC_DispStr( 0,  4, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  130.     PC_DispStr( 0,  5, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  131.     PC_DispStr( 0,  6, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  132.     PC_DispStr( 0,  7, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  133.     PC_DispStr( 0,  8, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  134.     PC_DispStr( 0,  9, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  135.     PC_DispStr( 0, 10, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  136.     PC_DispStr( 0, 11, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  137.     PC_DispStr( 0, 12, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  138.     PC_DispStr( 0, 13, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  139.     PC_DispStr( 0, 14, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  140.     PC_DispStr( 0, 15, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  141.     PC_DispStr( 0, 16, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  142.     PC_DispStr( 0, 17, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  143.     PC_DispStr( 0, 18, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  144.     PC_DispStr( 0, 19, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  145.     PC_DispStr( 0, 20, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  146.     PC_DispStr( 0, 21, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  147.     PC_DispStr( 0, 22, "#Tasks          :        CPU Usage:     %                                       ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  148.     PC_DispStr( 0, 23, "#Task switch/sec:                                                               ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  149.     PC_DispStr( 0, 24, "                            <-PRESS 'ESC' TO QUIT->                             ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK);
  150. /*                                1111111111222222222233333333334444444444555555555566666666667777777777 */
  151. /*                      01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
  152. }
  153.  
  154. /*$PAGE*/
  155. /*
  156. *********************************************************************************************************
  157. *                                           UPDATE THE DISPLAY
  158. *********************************************************************************************************
  159. */
  160.  
  161. static  void  TaskStartDisp (void)
  162. {
  163.     char   s[80];
  164.  
  165.  
  166.     sprintf(s, "%5d", OSTaskCtr);                                  /* Display #tasks running               */
  167.     PC_DispStr(18, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
  168.  
  169. #if OS_TASK_STAT_EN > 0
  170.     sprintf(s, "%3d", OSCPUUsage);                                 /* Display CPU usage in %               */
  171.     PC_DispStr(36, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
  172. #endif
  173.  
  174.     sprintf(s, "%5d", OSCtxSwCtr);                                 /* Display #context switches per second */
  175.     PC_DispStr(18, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
  176.  
  177.     sprintf(s, "V%1d.%02d", OSVersion() / 100, OSVersion() % 100); /* Display uC/OS-II's version number    */
  178.     PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
  179.  
  180.     switch (_8087) {                                               /* Display whether FPU present          */
  181.         case 0:
  182.              PC_DispStr(71, 22, " NO  FPU ", DISP_FGND_YELLOW + DISP_BGND_BLUE);
  183.              break;
  184.  
  185.         case 1:
  186.              PC_DispStr(71, 22, " 8087 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
  187.              break;
  188.  
  189.         case 2:
  190.              PC_DispStr(71, 22, "80287 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
  191.              break;
  192.  
  193.         case 3:
  194.              PC_DispStr(71, 22, "80387 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
  195.              break;
  196.     }
  197. }
  198.  
  199. /*$PAGE*/
  200. /*
  201. *********************************************************************************************************
  202. *                                             CREATE TASKS
  203. *********************************************************************************************************
  204. */
  205.  
  206. static  void  TaskStartCreateTasks (void)
  207. {
  208.     INT8U  i;
  209.  
  210.     OSTaskCreate(Keyboard, (void *)&KeyboardData, &TaskStk[TASK_STK_SIZE - 1], 1);
  211.     OSTaskCreate(Display, (void *)&DisplayData, &TaskStk[TASK_STK_SIZE - 1], 2);
  212.     for (i = 0; i < N_TASKS; i++) {                        /* Create N_TASKS identical tasks           */
  213.         TaskData[i] = '0' + i;                             /* Each task will display its own letter    */
  214.         OSTaskCreate(Task, (void *)&TaskData[i], &TaskStk[i][TASK_STK_SIZE - 1], i + 3);
  215.     }
  216. }
  217.  
  218. /*
  219. *********************************************************************************************************
  220. *                                                  TASKS
  221. *********************************************************************************************************
  222. */
  223.  
  224. void  Task (void *pdata)
  225. {
  226.     INT8U  x;
  227.     INT8U  y;
  228.     INT8U  err;
  229.  
  230.  
  231.     for (;;) {
  232.         OSSemPend(RandomSem, 0, &err);           /* Acquire semaphore to perform random numbers        */
  233.         x = random(80);                          /* Find X position where task number will appear      */
  234.         y = random(16);                          /* Find Y position where task number will appear      */
  235.         OSSemPost(RandomSem);                    /* Release semaphore                                  */
  236.                                                  /* Display the task number on the screen              */
  237.         PC_DispChar(x, y + 5, *(char *)pdata, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  238.         OSTimeDly(1);                            /* Delay 1 clock tick                                 */
  239.     }
  240. }
  241.  
  242. void  Keyboard (void *pdata)
  243. {
  244.     INT8U  x;
  245.     INT8U  y;
  246.     INT8U  err;
  247.  
  248.  
  249.     for (;;) {
  250.         OSSemPend(RandomSem, 0, &err);           /* Acquire semaphore to perform random numbers        */
  251.         x = random(80);                          /* Find X position where task number will appear      */
  252.         y = random(16);                          /* Find Y position where task number will appear      */
  253.         OSSemPost(RandomSem);                    /* Release semaphore                                  */
  254.                                                  /* Display the task number on the screen              */
  255.         PC_DispChar(x, y + 5, *(char *)pdata, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  256.         OSTimeDly(1);                            /* Delay 1 clock tick                                 */
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement