📄 cn_os.c
字号:
//==============================================
// :Cn.oS[Operating System from China]
//filename :Cn_oS.c
//description :the system's Core function(C programme part)
// :ProcessesControlBlock(PCB)
// :In #INT32UN *Init_Stack (void (*task),int task_id)#
// :include the default state of CPSR
//author :kaka(sn.kaka@yahoo.com )
//date :20080714
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//filename :Cn_oS.c
//description :Cn.oS的核心代码,C语言部分
// :ProcessesControlBlock(PCB)指的是进程控制块
// :汇编部分的代码也在当前目录下
// :
// :
// :In #INT32UN *Init_Stack (void (*task),int task_id)#
// :include the default state of CPSR
//author :kaka(sn.kaka@yahoo.com )
//date :20080714
//==============================================
#include "Cn_oS_head.h"
int *t;
INT32UN sum_critical ;
INT32UN *Current_Task_P;
INT32UN *Current_PCB_P;
INT32UN *Current_PCB_P_NEXT;
int Current_Task_ID=NULL;
int Task_Max=NULL;
int K_State=NULL; //if the variable EQU 0,show the K is sleep
int Time_out_temp;
int Time_out_must_do;
int Statistics_For_Load;
TINFO T_INFO[TASK_NUMBER];
char Cn_oS_Core_list[TASK_NUMBER];
int Core_list_finder_p=1;
//==============================================
// :Cn.oS[Operating System from China]
//function :it is only for debug to watch the stack pointer,
// :now it non't used
// :
// :
// :
// :
//author :kaka (sn.kaka@yahoo.com )
//data :20080730
// :
//==============================================
void Debug(void) //一个跟踪堆栈溢出的函数,一直没用过
{
int *p;
p=(int*)T_INFO[1].STACK;
}
//==============================================
// :Cn.oS[Operating System from China]
//function :Init the core list for Cn.oS,Cn_oS_Core_list[i]is import
// :it bear the weight of all message,such as delay time,mesage
// :the IDLE,the sleep.....
// :so you must Init it
// :in current edition,only the bit 0 is used for time_delay
// :
//author :kaka (sn.kaka@yahoo.com )
//data :20080730
// :
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//function :初始化核心表,Cn_oS_Core_list[i]相当重要
// :她里面包含着对应进程的相关信息
// :比方说延时过期,休眠,挂起
// :当然我目前只用到了其中的延时过期
// :这就是保证实时性能重要因素
// :后期可以和方便的将消息和休眠完善到
// :里面
//author :kaka (sn.kaka@yahoo.com )
//data :20080730
// :
//==============================================
void Init_Core_list(void)
{
int i;
for(i=0;i<TASK_NUMBER;i++)
{
Cn_oS_Core_list[i]=0x0;
}
}
//==============================================
// :Cn.oS[Operating System from China]
//function :Init the ProcessesControlBlock(PCB),not stack
// :now it named Init_Stack is a mistake,hou~~hou~~
// :it will init the CPU's register
// :
// :
// :
//author :kaka (sn.kaka@yahoo.com )
//data :20080718
// :
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//function :初始化进程控制块 ProcessesControlBlock(PCB)
// :不要故名思义而理解成堆栈,其实
// :这是我前期犯的一个小错误,不过
// :无所谓,后期可以完善
// :主要是初始化进程第一次运行时的寄存器
// :和当前程序状态寄存器CPSR
//author :kaka (sn.kaka@yahoo.com )
//data :20080718
// :
//==============================================
INT32UN *Init_Stack (void (*task),int task_id)
{
INT32UN *pcb;
pcb=T_INFO[task_id].PCB;
*pcb++=0x001f; /* CPSR SYS_mode,allow IRQ.FIQ .*/
*pcb++=(unsigned int)task; //PC point
*pcb++=0x0; //R0
*pcb++=0x1; //R1
*pcb++=0x2; //R2
*pcb++=0x3; //R3
*pcb++=0x4; //R4
*pcb++=0x5; //R5
*pcb++=0x6; //R6
*pcb++=0x7; //R7
*pcb++=0x8; //R8
*pcb++=0x9; //R9
*pcb++=0x10; //R10
*pcb++=0x11; //R11
*pcb++=0x12; //R12
*pcb++=(INT32UN )&(T_INFO[task_id].STACK[STACK_LENGTH-10]); //STACK,Not ProcessesControlBlock(PCB)
*pcb++=(unsigned int)task; //0x14; //R14
return(pcb);
}
//==============================================
// :Cn.oS[Operating System from China]
//function :create the task
//example :Task_Create( task_name,task_id);
/*void task_example(void *pdata)
{
pdata=pdata;
while(1)
{
do what you want###;
}
}
*/
//date :20080714
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//function :创建任务
//example :Task_Create( task_name,task_id);
/*void task_example(void *pdata)
{
pdata=pdata;
while(1)
{
do what you want###;
}
}
*/
//指针*pdata是保留的
//date :20080714
//==============================================
void Task_Create(void (*task)(void *pd),int task_id)
{
T_INFO[task_id].Stack_P=Init_Stack((INT32UN *)task,task_id);
T_INFO[task_id].Stack_P=T_INFO[task_id].PCB;
T_INFO[task_id].Task_ID=task_id;
T_INFO[task_id].Delay_tick=1;
}
//==============================================
// :Cn.oS[Operating System from China]
//function :creat a new Task_ID
// :she shoule do better the now!
//data :20080714
//data :20080716
// add the sentence I_ISPC |= (1 << 8);
// to clear the interrupt pending bit ,only on S3C44B0
// By kaka
//data :
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//function :调用该函数使当前任务延时一段时间
// :时间单位是系统节拍
// :目前只能延时自己,当然稍作改动
// :就可以延时其他任务,但是我觉得
// :应该加入相应的检测,使其只能延时
// :优先级比调用者低的任务,要是你想延时谁
// :就延时谁,想判谁死刑就判谁,那可了不敌了
// :呵呵~~~
//data :20080714
//data :20080716
// add the sentence I_ISPC |= (1 << 8);
// to clear the interrupt pending bit ,only on S3C44B0
// By kaka
//data :
//==============================================
void Cn_oS_Delay(int tick)
{ ENTER_CRITICAL();
Cn_oS_Core_list[Current_Task_ID]=(Cn_oS_Core_list[Current_Task_ID] |0x1);
T_INFO[Current_Task_ID].Delay_tick=tick;
TASK_SWITCH();
EXIT_CRITICAL();
}
//==============================================
// :Cn.oS[Operating System from China]
//function :it is not used now
// :
// :
// :
// :
// :
//author :kaka (sn.kaka@yahoo.com )
//data :20080725
// :
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//function :任务级别的时间管理,这个函数的诞生
// :本来就是个错误,一个相当严重的错误
// :留在这里起警示作用
// :
// :
// :
//author :kaka (sn.kaka@yahoo.com )
//data :20080725
// :
//==============================================
void Cn_oS_Delay_Manager(void)
{
int i;
for(i=1;i<TASK_NUMBER;i++)
{
if(T_INFO[i].Delay_tick>0)
{
T_INFO[i].Delay_tick--;
if(T_INFO[i].Delay_tick==0)
{
Cn_oS_Core_list[i]=Cn_oS_Core_list[i]&0xfe; //11111110
}
}
else
{
Time_out_must_do=Time_out_must_do;
}
}
}
//==============================================
// :Cn.oS[Operating System from China]
//function :Manager_For_Timeout
// :
//data :20080728
//data :20080729
//
//data :20080730
// :change it better
//==============================================
//==============================================
// :Cn.oS[Operating System from China]
//function :系统节拍管理,这个函数还可以个人
// :认为,因为每次时钟节拍来了之后
// :对延时进行递减式必须的,此函数在
// :递减过程中如果发现了有比当前任务
// :优先级高的任务的延时过期了,就
// :返回那个任务的ID,否则,返回的还是
// :目前正在运行的任务的ID
// :
//data :20080728
//data :20080729
//
//data :20080730
// :change it better
//==============================================
char Cn_oS_Delay_Manager_For_Timeout(void)
{
int i;char result=Current_Task_ID;
Time_out_temp=Current_Task_ID;
Time_out_must_do=0;
for(i=1;i<TASK_NUMBER;i++)
{
if(T_INFO[i].Delay_tick>0)
{
T_INFO[i].Delay_tick--;
if(T_INFO[i].Delay_tick==0)
{
Cn_oS_Core_list[i]=Cn_oS_Core_list[i]&0xfe; //11111110
if(Time_out_temp==0)
{
Time_out_temp=i;
result=i;
}
else{
if(Time_out_temp>i)
{
Time_out_temp=i;
result=i;
}
}
}
}
else
{
Time_out_must_do=Time_out_must_do;
T_INFO[i].Delay_tick=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -