/*************************uC/OS-II範例程式*******************************
* 檔名︰Test.C
* 功能︰等待若有按鍵BEEP嗶二聲
****************************************************************************/
#include "config.h"
#define TASK_STK_SIZE 128 // 設定每個任務的堆疊空間
#define N_TASKS 10 //設定任務數
OS_STK TaskStk[N_TASKS][TASK_STK_SIZE];
OS_STK TaskStartStk[TASK_STK_SIZE];
char TaskData[N_TASKS]; /* Parameters to pass to each task */
OS_EVENT *RandomSem;
void Task(void *data); // 宣告Task函數
void TaskStart(void *data); /* Function prototypes of Startup task */
static void TaskStartCreateTasks(void);
static void TaskStartDispInit(void);
static void TaskStartDisp(void);
//******************************************************************************
int main (void)
{
PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* Clear the screen */
OSInit(); // uC/OS-II啟始程式 /* Initialize uC/OS-II */
PC_DOSSaveReturn(); /* Save environment to return to DOS */
RandomSem = OSSemCreate(1); /* Random number semaphore */
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1],
0);//建立啟始程式任務
OSStart(); // 開始多工任務執行/* Start multitasking */
}
//*********************************************************************************************************
void TaskStart (void *pdata) // 啟始程式任務
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT16S key;
pdata = pdata; /* Prevent compiler warning */
TaskStartDispInit(); /* Initialize the display */
TargetInit(); //add by cmj
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 */
}
}
/**********************************************************************************************************
* 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 */
}
}
/*******************uC/OS-II與多任務使用範例程式**************
* 檔名︰MAIN.C
* 功能︰的多任務使用,在電腦顯示顯示三個任務執行次數
* 操作:先執行人機界面easyarm軟體,觀察三個任務執行次數
* 接線︰將JP2短路。
*******************************************************************/
#include "config.h"
#define TASK_STK_SIZE 64
OS_STK TaskStartStk[TASK_STK_SIZE];
OS_STK Task1Stk[TASK_STK_SIZE];
OS_STK Task2Stk[TASK_STK_SIZE];
void TaskStart(void *data);
void Task1(void *data);
void Task2(void *data);
/******************************************************************
** 函數名稱: main
** 功能描述: c語言的主函數,由它啟動多任務環境
** 呼叫函數: OSInit,OSTaskCreate,OSStart
***************************************************************/
int main (void)
{
OSInit();
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);
OSStart();
return 0;
}
/****************************************************************
** 函數名稱: TaskStart
** 功能描述: 顯示任務1的執行次數
******************************************************************/
void TaskStart(void *pdata)
{
char s[40];
uint16 count1;
pdata = pdata; /* 避免編譯警告 */
TargetInit(); /* 目標板初始化 */
OSTaskCreate(Task1, (void *)0, &Task1Stk[TASK_STK_SIZE - 1], 10);
OSTaskCreate(Task2, (void *)0, &Task2Stk[TASK_STK_SIZE - 1], 11);
count1=0;
PC_DispClrScr (0x70); // 清DOS除畫面
for (;;) //重覆執行
{
sprintf(s, "TASK0=%05d",count1); // 格式轉換
PC_DispStr(10, 1, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);// 顯示次數
count1++; // 次數遞加
if(count1==65535) count1=0; // 次數遞加限於0~65535
OSTimeDly(OS_TICKS_PER_SEC /4); // 延時0.25秒
}
}
/*********************************************************************
** 函數名稱: Task1
** 功能描述: 顯示任務1的執行次數
*********************************************************************/
void Task1(void *pdata)
{
char s[40];
uint16 count1;
pdata = pdata; /* 避免編譯警告 */
count1=0; //重覆執行
for (;;)
{
sprintf(s, "TASK1=%05d",count1); // 格式轉換
PC_DispStr(10, 3, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);// 顯示次數
count1++; // 次數遞加
if(count1==65535) count1=0; // 次數遞加限於0~65535
OSTimeDly(OS_TICKS_PER_SEC/2); // 延時0.5秒
}
}
/***************************************************************
** 函數名稱: Task2
** 功能描述: 顯示任務2的執行次數
*************************************************************/
void Task2(void *pdata)
{
char s[40];
uint16 count1;
pdata = pdata; /* 避免編譯警告 */
count1=0;
for (;;) //重覆執行
{
sprintf(s, "TASK2=%05d",count1); // 格式轉換
PC_DispStr(10, 5, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);// 顯示次數
count1++; // 次數遞加
if(count1==65535) count1=0; //次數遞加限於0~65535
OSTimeDly(OS_TICKS_PER_SEC); // 延時1秒
}
}