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 35 /* 36 ********************************************************************************************************* 37 * FUNCTION PROTOTYPES 38 ********************************************************************************************************* 39 */ 40 41 void Task(void *data); /* Function prototypes of tasks */ 42 void TaskStart(void *data); /* Function prototypes of Startup task */ 43 static void TaskStartCreateTasks(void); 44 static void TaskStartDispInit(void); 45 static void TaskStartDisp(void); 46 47 /*$PAGE*/ 48 /* 49 ********************************************************************************************************* 50 * MAIN 51 ********************************************************************************************************* 52 */ 53 54 void main (void) 55 { 56 PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* Clear the screen */ 57 58 OSInit(); /* Initialize uC/OS-II */ 59 60 PC_DOSSaveReturn(); /* Save environment to return to DOS */ 61 PC_VectSet(uCOS, OSCtxSw); /* Install uC/OS-II's context switch vector */ 62 63 RandomSem = OSSemCreate(1); /* Random number semaphore */ 64 65 OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0); 66 OSStart(); /* Start multitasking */ 67 } 68 69 70 /* 71 ********************************************************************************************************* 72 * STARTUP TASK 73 ********************************************************************************************************* 74 */ 75 void TaskStart (void *pdata) 76 { 77 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ 78 OS_CPU_SR cpu_sr; 79 #endif 80 char s[100]; 81 INT16S key; 82 83 84 pdata = pdata; /* Prevent compiler warning */ 85 86 TaskStartDispInit(); /* Initialize the display */ 87 88 OS_ENTER_CRITICAL(); 89 PC_VectSet(0x08, OSTickISR); /* Install uC/OS-II's clock tick ISR */ 90 PC_SetTickRate(OS_TICKS_PER_SEC); /* Reprogram tick rate */ 91 OS_EXIT_CRITICAL(); 92 93 OSStatInit(); /* Initialize uC/OS-II's statistics */ 94 95 TaskStartCreateTasks(); /* Create all the application tasks */ 96 97 for (;;) { 98 TaskStartDisp(); /* Update the display */ 99 100 101 if (PC_GetKey(&key) == TRUE) { /* See if key has been pressed */ 102 if (key == 0x1B) { /* Yes, see if it's the ESCAPE key */ 103 PC_DOSReturn(); /* Return to DOS */ 104 } 105 } 106 107 OSCtxSwCtr = 0; /* Clear context switch counter */ 108 OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */ 109 } 110 } 111 112 /*$PAGE*/ 113 /* 114 ********************************************************************************************************* 115 * INITIALIZE THE DISPLAY 116 ********************************************************************************************************* 117 */ 118 119 static void TaskStartDispInit (void) 120 { 121 /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ 122 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ 123 PC_DispStr( 0, 0, " uC/OS-II, The Real-Time Kernel ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); 124 PC_DispStr( 0, 1, " Jean J. Labrosse ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 125 PC_DispStr( 0, 2, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 126 PC_DispStr( 0, 3, " EXAMPLE #1 ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 127 PC_DispStr( 0, 4, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 128 PC_DispStr( 0, 5, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 129 PC_DispStr( 0, 6, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 130 PC_DispStr( 0, 7, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 131 PC_DispStr( 0, 8, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 132 PC_DispStr( 0, 9, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 133 PC_DispStr( 0, 10, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 134 PC_DispStr( 0, 11, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 135 PC_DispStr( 0, 12, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 136 PC_DispStr( 0, 13, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 137 PC_DispStr( 0, 14, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 138 PC_DispStr( 0, 15, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 139 PC_DispStr( 0, 16, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 140 PC_DispStr( 0, 17, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 141 PC_DispStr( 0, 18, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 142 PC_DispStr( 0, 19, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 143 PC_DispStr( 0, 20, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 144 PC_DispStr( 0, 21, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 145 PC_DispStr( 0, 22, "#Tasks : CPU Usage: % ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 146 PC_DispStr( 0, 23, "#Task switch/sec: ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 147 PC_DispStr( 0, 24, " <-PRESS 'ESC' TO QUIT-> ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK); 148 /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ 149 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ 150 } 151 152 /*$PAGE*/ 153 /* 154 ********************************************************************************************************* 155 * UPDATE THE DISPLAY 156 ********************************************************************************************************* 157 */ 158 159 static void TaskStartDisp (void) 160 { 161 char s[80]; 162 163 164 sprintf(s, "%5d", OSTaskCtr); /* Display #tasks running */ 165 PC_DispStr(18, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); 166 167 #if OS_TASK_STAT_EN > 0 168 sprintf(s, "%3d", OSCPUUsage); /* Display CPU usage in % */ 169 PC_DispStr(36, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); 170 #endif 171 172 sprintf(s, "%5d", OSCtxSwCtr); /* Display #context switches per second */ 173 PC_DispStr(18, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); 174 175 sprintf(s, "V%1d.%02d", OSVersion() / 100, OSVersion() % 100); /* Display uC/OS-II's version number */ 176 PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); 177 178 switch (_8087) { /* Display whether FPU present */ 179 case 0: 180 PC_DispStr(71, 22, " NO FPU ", DISP_FGND_YELLOW + DISP_BGND_BLUE); 181 break; 182 183 case 1: 184 PC_DispStr(71, 22, " 8087 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE); 185 break; 186 187 case 2: 188 PC_DispStr(71, 22, "80287 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE); 189 break; 190 191 case 3: 192 PC_DispStr(71, 22, "80387 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE); 193 break; 194 } 195 } 196 197 /*$PAGE*/ 198 /* 199 ********************************************************************************************************* 200 * CREATE TASKS 201 ********************************************************************************************************* 202 */ 203 204 static void TaskStartCreateTasks (void) 205 { 206 INT8U i; 207 208 209 for (i = 0; i < N_TASKS; i++) { /* Create N_TASKS identical tasks */ 210 TaskData[i] = '0' + i; /* Each task will display its own letter */ 211 OSTaskCreate(Task, (void *)&TaskData[i], &TaskStk[i][TASK_STK_SIZE - 1], i + 1); 212 } 213 } 214 215 /* 216 ********************************************************************************************************* 217 * TASKS 218 ********************************************************************************************************* 219 */ 220 221 void Task (void *pdata) 222 { 223 INT8U x; 224 INT8U y; 225 INT8U err; 226 227 228 for (;;) { 229 OSSemPend(RandomSem, 0, &err); /* Acquire semaphore to perform random numbers */ 230 x = random(80); /* Find X position where task number will appear */ 231 y = random(16); /* Find Y position where task number will appear */ 232 OSSemPost(RandomSem); /* Release semaphore */ 233 /* Display the task number on the screen */ 234 PC_DispChar(x, y + 5, *(char *)pdata, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); 235 OSTimeDly(1); /* Delay 1 clock tick */ 236 } 237 }
1 /* 2 ********************************************************************************************************* 3 * uC/OS-II 4 * The Real-Time Kernel 5 * 6 * (c) Copyright 1992-2001, Jean J. Labrosse, Weston, FL 7 * All Rights Reserved 8 * 9 * uC/OS-II Configuration File for V2.51 10 * 11 * File : OS_CFG.H 12 * By : Jean J. Labrosse 13 ********************************************************************************************************* 14 */ 15 16 /* 17 ********************************************************************************************************* 18 * uC/OS-II CONFIGURATION 19 ********************************************************************************************************* 20 */ 21 22 #define OS_MAX_EVENTS 2 /* Max. number of event control blocks in your application ... */ 23 /* ... MUST be > 0 */ 24 #define OS_MAX_FLAGS 5 /* Max. number of Event Flag Groups in your application ... */ 25 /* ... MUST be > 0 */ 26 #define OS_MAX_MEM_PART 5 /* Max. number of memory partitions ... */ 27 /* ... MUST be > 0 */ 28 #define OS_MAX_QS 2 /* Max. number of queue control blocks in your application ... */ 29 /* ... MUST be > 0 */ 30 #define OS_MAX_TASKS 11 /* Max. number of tasks in your application ... */ 31 /* ... MUST be >= 2 */ 32 33 #define OS_LOWEST_PRIO 12 /* Defines the lowest priority that can be assigned ... */ 34 /* ... MUST NEVER be higher than 63! */ 35 36 #define OS_TASK_IDLE_STK_SIZE 512 /* Idle task stack size (# of OS_STK wide entries) */ 37 38 #define OS_TASK_STAT_EN 1 /* Enable (1) or Disable(0) the statistics task */ 39 #define OS_TASK_STAT_STK_SIZE 512 /* Statistics task stack size (# of OS_STK wide entries) */ 40 41 #define OS_ARG_CHK_EN 1 /* Enable (1) or Disable (0) argument checking */ 42 #define OS_CPU_HOOKS_EN 1 /* uC/OS-II hooks are found in the processor port files */ 43 44 45 /* ----------------------- EVENT FLAGS ------------------------ */ 46 #define OS_FLAG_EN 1 /* Enable (1) or Disable (0) code generation for EVENT FLAGS */ 47 #define OS_FLAG_WAIT_CLR_EN 1 /* Include code for Wait on Clear EVENT FLAGS */ 48 #define OS_FLAG_ACCEPT_EN 1 /* Include code for OSFlagAccept() */ 49 #define OS_FLAG_DEL_EN 1 /* Include code for OSFlagDel() */ 50 #define OS_FLAG_QUERY_EN 1 /* Include code for OSFlagQuery() */ 51 52 53 /* -------------------- MESSAGE MAILBOXES --------------------- */ 54 #define OS_MBOX_EN 1 /* Enable (1) or Disable (0) code generation for MAILBOXES */ 55 #define OS_MBOX_ACCEPT_EN 1 /* Include code for OSMboxAccept() */ 56 #define OS_MBOX_DEL_EN 1 /* Include code for OSMboxDel() */ 57 #define OS_MBOX_POST_EN 1 /* Include code for OSMboxPost() */ 58 #define OS_MBOX_POST_OPT_EN 1 /* Include code for OSMboxPostOpt() */ 59 #define OS_MBOX_QUERY_EN 1 /* Include code for OSMboxQuery() */ 60 61 62 /* --------------------- MEMORY MANAGEMENT -------------------- */ 63 #define OS_MEM_EN 1 /* Enable (1) or Disable (0) code generation for MEMORY MANAGER */ 64 #define OS_MEM_QUERY_EN 1 /* Include code for OSMemQuery() */ 65 66 67 /* ---------------- MUTUAL EXCLUSION SEMAPHORES --------------- */ 68 #define OS_MUTEX_EN 1 /* Enable (1) or Disable (0) code generation for MUTEX */ 69 #define OS_MUTEX_ACCEPT_EN 1 /* Include code for OSMutexAccept() */ 70 #define OS_MUTEX_DEL_EN 1 /* Include code for OSMutexDel() */ 71 #define OS_MUTEX_QUERY_EN 1 /* Include code for OSMutexQuery() */ 72 73 74 /* ---------------------- MESSAGE QUEUES ---------------------- */ 75 #define OS_Q_EN 1 /* Enable (1) or Disable (0) code generation for QUEUES */ 76 #define OS_Q_ACCEPT_EN 1 /* Include code for OSQAccept() */ 77 #define OS_Q_DEL_EN 1 /* Include code for OSQDel() */ 78 #define OS_Q_FLUSH_EN 1 /* Include code for OSQFlush() */ 79 #define OS_Q_POST_EN 1 /* Include code for OSQPost() */ 80 #define OS_Q_POST_FRONT_EN 1 /* Include code for OSQPostFront() */ 81 #define OS_Q_POST_OPT_EN 1 /* Include code for OSQPostOpt() */ 82 #define OS_Q_QUERY_EN 1 /* Include code for OSQQuery() */ 83 84 85 /* ------------------------ SEMAPHORES ------------------------ */ 86 #define OS_SEM_EN 1 /* Enable (1) or Disable (0) code generation for SEMAPHORES */ 87 #define OS_SEM_ACCEPT_EN 1 /* Include code for OSSemAccept() */ 88 #define OS_SEM_DEL_EN 1 /* Include code for OSSemDel() */ 89 #define OS_SEM_QUERY_EN 1 /* Include code for OSSemQuery() */ 90 91 92 /* --------------------- TASK MANAGEMENT ---------------------- */ 93 #define OS_TASK_CHANGE_PRIO_EN 1 /* Include code for OSTaskChangePrio() */ 94 #define OS_TASK_CREATE_EN 1 /* Include code for OSTaskCreate() */ 95 #define OS_TASK_CREATE_EXT_EN 1 /* Include code for OSTaskCreateExt() */ 96 #define OS_TASK_DEL_EN 1 /* Include code for OSTaskDel() */ 97 #define OS_TASK_SUSPEND_EN 1 /* Include code for OSTaskSuspend() and OSTaskResume() */ 98 #define OS_TASK_QUERY_EN 1 /* Include code for OSTaskQuery() */ 99 100 101 /* --------------------- TIME MANAGEMENT ---------------------- */ 102 #define OS_TIME_DLY_HMSM_EN 1 /* Include code for OSTimeDlyHMSM() */ 103 #define OS_TIME_DLY_RESUME_EN 1 /* Include code for OSTimeDlyResume() */ 104 #define OS_TIME_GET_SET_EN 1 /* Include code for OSTimeGet() and OSTimeSet() */ 105 106 107 /* ---------------------- MISCELLANEOUS ----------------------- */ 108 #define OS_SCHED_LOCK_EN 1 /* Include code for OSSchedLock() and OSSchedUnlock() */ 109 110 111 #define OS_TICKS_PER_SEC 200 /* Set the number of ticks in one second */ 112 113 114 typedef INT16U OS_FLAGS; /* Date type for event flag bits (8, 16 or 32 bits) */