uC/OS-II 范例1
源码
1.include.h
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
* (c) Copyright 1992-1999, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* MASTER INCLUDE FILE
*********************************************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <setjmp.h>
#include "\software\ucos-ii\ix86l\bc45\os_cpu.h"
#include "os_cfg.h"
#include "\software\ucos-ii\source\ucos_ii.h"
#include "\software\blocks\pc\bc45\pc.h"
2.os_CFG.h
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
* (c) Copyright 1992-2001, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* uC/OS-II Configuration File for V2.51
*
* File : OS_CFG.H
* By : Jean J. Labrosse
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* uC/OS-II CONFIGURATION
*********************************************************************************************************
*/
#define OS_MAX_EVENTS 2 /* Max. number of event control blocks in your application ... */
/* ... MUST be > 0 */
#define OS_MAX_FLAGS 5 /* Max. number of Event Flag Groups in your application ... */
/* ... MUST be > 0 */
#define OS_MAX_MEM_PART 5 /* Max. number of memory partitions ... */
/* ... MUST be > 0 */
#define OS_MAX_QS 2 /* Max. number of queue control blocks in your application ... */
/* ... MUST be > 0 */
#define OS_MAX_TASKS 11 /* Max. number of tasks in your application ... */
/* ... MUST be >= 2 */
#define OS_LOWEST_PRIO 12 /* Defines the lowest priority that can be assigned ... */
/* ... MUST NEVER be higher than 63! */
#define OS_TASK_IDLE_STK_SIZE 512 /* Idle task stack size (# of OS_STK wide entries) */
#define OS_TASK_STAT_EN 1 /* Enable (1) or Disable(0) the statistics task */
#define OS_TASK_STAT_STK_SIZE 512 /* Statistics task stack size (# of OS_STK wide entries) */
#define OS_ARG_CHK_EN 1 /* Enable (1) or Disable (0) argument checking */
#define OS_CPU_HOOKS_EN 1 /* uC/OS-II hooks are found in the processor port files */
/* ----------------------- EVENT FLAGS ------------------------ */
#define OS_FLAG_EN 1 /* Enable (1) or Disable (0) code generation for EVENT FLAGS */
#define OS_FLAG_WAIT_CLR_EN 1 /* Include code for Wait on Clear EVENT FLAGS */
#define OS_FLAG_ACCEPT_EN 1 /* Include code for OSFlagAccept() */
#define OS_FLAG_DEL_EN 1 /* Include code for OSFlagDel() */
#define OS_FLAG_QUERY_EN 1 /* Include code for OSFlagQuery() */
/* -------------------- MESSAGE MAILBOXES --------------------- */
#define OS_MBOX_EN 1 /* Enable (1) or Disable (0) code generation for MAILBOXES */
#define OS_MBOX_ACCEPT_EN 1 /* Include code for OSMboxAccept() */
#define OS_MBOX_DEL_EN 1 /* Include code for OSMboxDel() */
#define OS_MBOX_POST_EN 1 /* Include code for OSMboxPost() */
#define OS_MBOX_POST_OPT_EN 1 /* Include code for OSMboxPostOpt() */
#define OS_MBOX_QUERY_EN 1 /* Include code for OSMboxQuery() */
/* --------------------- MEMORY MANAGEMENT -------------------- */
#define OS_MEM_EN 1 /* Enable (1) or Disable (0) code generation for MEMORY MANAGER */
#define OS_MEM_QUERY_EN 1 /* Include code for OSMemQuery() */
/* ---------------- MUTUAL EXCLUSION SEMAPHORES --------------- */
#define OS_MUTEX_EN 1 /* Enable (1) or Disable (0) code generation for MUTEX */
#define OS_MUTEX_ACCEPT_EN 1 /* Include code for OSMutexAccept() */
#define OS_MUTEX_DEL_EN 1 /* Include code for OSMutexDel() */
#define OS_MUTEX_QUERY_EN 1 /* Include code for OSMutexQuery() */
/* ---------------------- MESSAGE QUEUES ---------------------- */
#define OS_Q_EN 1 /* Enable (1) or Disable (0) code generation for QUEUES */
#define OS_Q_ACCEPT_EN 1 /* Include code for OSQAccept() */
#define OS_Q_DEL_EN 1 /* Include code for OSQDel() */
#define OS_Q_FLUSH_EN 1 /* Include code for OSQFlush() */
#define OS_Q_POST_EN 1 /* Include code for OSQPost() */
#define OS_Q_POST_FRONT_EN 1 /* Include code for OSQPostFront() */
#define OS_Q_POST_OPT_EN 1 /* Include code for OSQPostOpt() */
#define OS_Q_QUERY_EN 1 /* Include code for OSQQuery() */
/* ------------------------ SEMAPHORES ------------------------ */
#define OS_SEM_EN 1 /* Enable (1) or Disable (0) code generation for SEMAPHORES */
#define OS_SEM_ACCEPT_EN 1 /* Include code for OSSemAccept() */
#define OS_SEM_DEL_EN 1 /* Include code for OSSemDel() */
#define OS_SEM_QUERY_EN 1 /* Include code for OSSemQuery() */
/* --------------------- TASK MANAGEMENT ---------------------- */
#define OS_TASK_CHANGE_PRIO_EN 1 /* Include code for OSTaskChangePrio() */
#define OS_TASK_CREATE_EN 1 /* Include code for OSTaskCreate() */
#define OS_TASK_CREATE_EXT_EN 1 /* Include code for OSTaskCreateExt() */
#define OS_TASK_DEL_EN 1 /* Include code for OSTaskDel() */
#define OS_TASK_SUSPEND_EN 1 /* Include code for OSTaskSuspend() and OSTaskResume() */
#define OS_TASK_QUERY_EN 1 /* Include code for OSTaskQuery() */
/* --------------------- TIME MANAGEMENT ---------------------- */
#define OS_TIME_DLY_HMSM_EN 1 /* Include code for OSTimeDlyHMSM() */
#define OS_TIME_DLY_RESUME_EN 1 /* Include code for OSTimeDlyResume() */
#define OS_TIME_GET_SET_EN 1 /* Include code for OSTimeGet() and OSTimeSet() */
/* ---------------------- MISCELLANEOUS ----------------------- */
#define OS_SCHED_LOCK_EN 1 /* Include code for OSSchedLock() and OSSchedUnlock() */
#define OS_TICKS_PER_SEC 200 /* Set the number of ticks in one second */
typedef INT16U OS_FLAGS; /* Date type for event flag bits (8, 16 or 32 bits) */
3 test.c
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
* (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* EXAMPLE #1
*********************************************************************************************************
*/
#include "includes.h"
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
#define TASK_STK_SIZE 512 /* Size of each task's stacks (# of WORDs) */
#define N_TASKS 10 /* Number of identical tasks */
/*
*********************************************************************************************************
* VARIABLES
*********************************************************************************************************
*/
OS_STK TaskStk[N_TASKS][TASK_STK_SIZE]; /* Tasks stacks */
OS_STK TaskStartStk[TASK_STK_SIZE];
char TaskData[N_TASKS]; /* Parameters to pass to each task */
OS_EVENT *RandomSem;
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
void Task(void *data); /* Function prototypes of tasks */
void TaskStart(void *data); /* Function prototypes of Startup task */
static void TaskStartCreateTasks(void);
static void TaskStartDispInit(void);
static void TaskStartDisp(void);
/*$PAGE*/
/*
*********************************************************************************************************
* MAIN
*********************************************************************************************************
*/
void main (void)
{
PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* Clear the screen */
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 */
RandomSem = OSSemCreate(1); /* Random number semaphore */
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);
OSStart(); /* Start multitasking */
}
/*
*********************************************************************************************************
* STARTUP TASK
*********************************************************************************************************
*/
void TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
char s[100];
INT16S key;
pdata = pdata; /* Prevent compiler warning */
TaskStartDispInit(); /* Initialize the display */
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 */
TaskStartCreateTasks(); /* Create all the application tasks */
for (;;) {
TaskStartDisp(); /* Update the display */
if (PC_GetKey(&key) == TRUE) { /* See if key has been pressed */
if (key == 0x1B) { /* Yes, see if it's the ESCAPE key */
PC_DOSReturn(); /* Return to DOS */
}
}
OSCtxSwCtr = 0; /* Clear context switch counter */
OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZE THE DISPLAY
*********************************************************************************************************
*/
static void TaskStartDispInit (void)
{
/* 1111111111222222222233333333334444444444555555555566666666667777777777 */
/* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
PC_DispStr( 0, 0, " uC/OS-II, The Real-Time Kernel ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
PC_DispStr( 0, 1, " Jean J. Labrosse ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 2, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 3, " EXAMPLE #1 ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 4, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 5, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 6, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 7, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 8, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 9, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 10, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 11, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 12, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 13, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 14, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 15, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 16, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 17, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 18, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 19, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 20, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 21, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 22, "#Tasks : CPU Usage: % ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 23, "#Task switch/sec: ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr( 0, 24, " <-PRESS 'ESC' TO QUIT-> ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK);
/* 1111111111222222222233333333334444444444555555555566666666667777777777 */
/* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
}
/*$PAGE*/
/*
*********************************************************************************************************
* UPDATE THE DISPLAY
*********************************************************************************************************
*/
static void TaskStartDisp (void)
{
char s[80];
sprintf(s, "%5d", OSTaskCtr); /* Display #tasks running */
PC_DispStr(18, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
#if OS_TASK_STAT_EN > 0
sprintf(s, "%3d", OSCPUUsage); /* Display CPU usage in % */
PC_DispStr(36, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
#endif
sprintf(s, "%5d", OSCtxSwCtr); /* Display #context switches per second */
PC_DispStr(18, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
sprintf(s, "V%1d.%02d", OSVersion() / 100, OSVersion() % 100); /* Display uC/OS-II's version number */
PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
switch (_8087) { /* Display whether FPU present */
case 0:
PC_DispStr(71, 22, " NO FPU ", DISP_FGND_YELLOW + DISP_BGND_BLUE);
break;
case 1:
PC_DispStr(71, 22, " 8087 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
break;
case 2:
PC_DispStr(71, 22, "80287 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
break;
case 3:
PC_DispStr(71, 22, "80387 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
break;
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CREATE TASKS
*********************************************************************************************************
*/
static void TaskStartCreateTasks (void)
{
INT8U i;
for (i = 0; i < N_TASKS; i++) { /* Create N_TASKS identical tasks */
TaskData[i] = '0' + i; /* Each task will display its own letter */
OSTaskCreate(Task, (void *)&TaskData[i], &TaskStk[i][TASK_STK_SIZE - 1], i + 1);
}
}
/*
*********************************************************************************************************
* TASKS
*********************************************************************************************************
*/
void Task (void *pdata)
{
INT8U x;
INT8U y;
INT8U err;
for (;;) {
OSSemPend(RandomSem, 0, &err); /* Acquire semaphore to perform random numbers */
x = random(80); /* Find X position where task number will appear */
y = random(16); /* Find Y position where task number will appear */
OSSemPost(RandomSem); /* Release semaphore */
/* Display the task number on the screen */
PC_DispChar(x, y + 5, *(char *)pdata, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
OSTimeDly(1); /* Delay 1 clock tick */
}
}