⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_cpu_c.c

📁 基于51单片机UCOS移植
💻 C
字号:
/*
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*
*                          (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
*                                           All Rights Reserved
*
*
*                                           uCOS_51 for MCS-51
*
* File : OS_CPU_C.C
* By   : Jean J. Labrosse
* Created by : QQ 591881218
*********************************************************************************************************
*/

#define  OS_CPU_GLOBALS

#include "..\ucos_51\ucos-ii\inc\includes.h"

/*
*********************************************************************************************************
*                                             初始化任务栈
*
* 描述       : 本函数为OSTaskCreate()和OSTaskCreateExt()函数所调用,用于任务栈的初始化。初始化后的任务栈
*			   看起来像刚刚发生过一次中断并将所有的寄存器都保存进了堆栈的情形一样。
*
* 参数       : task		任务代码的指针。
*
*			   ppdata	当任务开始执行时传递给任务的参数的指针。
*
*			   ptos		分配给任务堆栈的栈顶指针。
*
*			   opt		用于设定OSTaskCreateExt()的选项,指定是否允许堆栈检验,是否将堆栈清零,是否进行
*						浮点操作等。当用OSTaskCreate()函数调用时,设置为0。
*
* 返回值	 : 返回栈顶指针
* 
* 注意       : 任务栈结构示意图(杨屹)
*
*                                    ---------- -
*                 用户栈最高地址---->|        | |
*                                    ---------- |
*                                    |   ...  | 仿真堆栈空间
*----------                          ---------- | 每任务一个
*|OSTCBCur|               ?C_XBP---->|        | | KEIL自动处理
*----------                          ---------- -
*    |                               |空闲间隔|
*    |     -----------------------   ----------                           ----------
*    \---->|OSTCBCur->OSTCBStkPtr|   |?C_XBP低|                    SP---->|        |
*          -----------------------   ----------                           ----------
*                     |              |?C_XBP高|                           |        |
*                     |              ---------- -                         ----------
*                     |              |        | |                         |   .    |
*                     |              ---------- |                         |   .    |
*                     |              |        | |                         |   .    |
*                     |              ---------- |                         ----------
*                     |              |   .    |长度                       |        | +1
*                     |              |   .    | |                         ----------
*                     |              |   .    | |             OSStack---->|        | 0
*                     |              ---------- |                         ----------
*                     |              |        | |          OSStkStart---->| 不关心 | -1  低地址
*                     |              ---------- -                         ----------
*                     \------------->|  长度  | 低地址                   系统硬件堆栈
*                                    ----------
*                                     用户堆栈                       长度=SP-OSStkStart
*********************************************************************************************************
*/
void *OSTaskStkInit(void(*task)(void *pd), void *ppdata, void *ptos, INT16U opt) REENTRANT
{
	OS_STK *stk;
	ppdata=ppdata;
	opt	  =opt;						/* opt没被用到,保留此语句防止警告产生 			*/
	stk	  =(OS_STK *)ptos;			/* 任务堆栈最低有效地址 						*/
	*stk++=15;						/* 任务堆栈长度 								*/
	*stk++=(INT16U)task & 0xFF;		/* 任务代码地址低8位 							*/
	*stk++=(INT16U)task >> 8;		/* 任务代码地址高8位 							*/
	/* 处理器是按特定的顺序将寄存器存入堆栈的,所以用户在将寄存器存入堆栈的时候也要依照这一顺序 */
	*stk++=0x00;					/* PSW 											*/
	*stk++=0x0A;					/* ACC 											*/
	*stk++=0x0B;					/* B 											*/
	*stk++=0x00;					/* DPL 											*/
	*stk++=0x00;					/* DPH 											*/
	*stk++=0x00;					/* R0 											*/
	*stk++=0x01;					/* R1 											*/
	*stk++=0x02;					/* R2 											*/
	*stk++=0x03;					/* R3 											*/	
	*stk++=0x04;					/* R4 											*/
	*stk++=0x05;					/* R5 											*/
	*stk++=0x06;					/* R6 											*/
	*stk++=0x07;					/* R7 											*/
	/* 不用保存SP,任务切换时根据用户堆栈长度计算得出 								*/
	*stk++=(INT16U)(ptos+MAX_STK_SIZE) >> 8;	/* ?C_XBP 仿真堆栈指针高8位 		*/
	*stk++=(INT16U)(ptos+MAX_STK_SIZE) & 0xFF;	/* ?C_XBP 仿真堆栈低8位 			*/
	return ((void *)ptos);	/* 返回最低地址,这里不用弹出栈顶指针是为了提高计算效率 */
}

/*********************************************** 钩子函数 ***********************************************/

#if OS_CPU_HOOKS_EN
/*
*********************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                            (BEGINNING)
*
* Description: This function is called by OSInit() at the beginning of OSInit().
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/
#if OS_VERSION > 203
void OSInitHookBegin (void)
{
}
#endif

/*
*********************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                               (END)
*
* Description: This function is called by OSInit() at the end of OSInit().
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/
#if OS_VERSION > 203
void OSInitHookEnd (void)
{
}
#endif

/*
*********************************************************************************************************
*                                          TASK CREATION HOOK
*
* Description: This function is called when a task is created.
*
* Arguments  : ptcb   is a pointer to the task control block of the task being created.
*
* Note(s)    : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
void OSTaskCreateHook (OS_TCB *ptcb) REENTRANT
{
    ptcb = ptcb;                       /* Prevent compiler warning                                     */
}

/*
*********************************************************************************************************
*                                           TASK DELETION HOOK
*
* Description: This function is called when a task is deleted.
*
* Arguments  : ptcb   is a pointer to the task control block of the task being deleted.
*
* Note(s)    : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
void OSTaskDelHook (OS_TCB *ptcb) REENTRANT
{
    ptcb = ptcb;                       /* Prevent compiler warning                                     */
}

/*
*********************************************************************************************************
*                                           TASK SWITCH HOOK
*
* Description: This function is called when a task switch is performed.  This allows you to perform other
*              operations during a context switch.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts are disabled during this call.
*              2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
*                 will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the 
*                 task being switched out (i.e. the preempted task).
*********************************************************************************************************
*/
void OSTaskSwHook (void) REENTRANT
{ 
 /* P1=0xfe;
	Delay();
	P1=0x7f;
	Delay();
			   */
}

/*
*********************************************************************************************************
*                                           STATISTIC TASK HOOK
*
* Description: This function is called every second by uC/OS-II's statistics task.  This allows your 
*              application to add functionality to the statistics task.
*
* Arguments  : none
*********************************************************************************************************
*/
void OSTaskStatHook (void) REENTRANT
{
}

/*
*********************************************************************************************************
*                                           OSTCBInit() HOOK
*
* Description: This function is called by OSTCBInit() after setting up most of the TCB.
*
* Arguments  : ptcb    is a pointer to the TCB of the task being created.
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
#if OS_VERSION > 203
void OSTCBInitHook (OS_TCB *ptcb) REENTRANT
{
    ptcb = ptcb;                                           /* Prevent Compiler warning                 */
}
#endif

/*
*********************************************************************************************************
*                                               TICK HOOK
*
* Description: This function is called every tick.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
void OSTimeTickHook (void) REENTRANT
{
/*	P1=0xfe;
	Delay();
	P1=0x7f;
	Delay();	*/
}

/*
*********************************************************************************************************
*                                             IDLE TASK HOOK
*
* Description: This function is called by the idle task.  This hook has been added to allow you to do  
*              such things as STOP the CPU to conserve power.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts are enabled during this call.
*********************************************************************************************************
*/
#if OS_VERSION >= 251
void OSTaskIdleHook (void) REENTRANT
{
/*	P1=0;
	P1=0xfe;
	Delay();
	P1=0x7f;
	Delay(); */
}
#endif

#endif

/********************************************************************************************************/

/* 初始化定时器0,用于产生时钟节拍 */
void InitTimer0(void) REENTRANT
{
    TMOD=TMOD&0xF0;
    TMOD=TMOD|0x01;	/* 模式1(16位定时器),仅受TR0控制 */
	TH0=TIMER_20MS_TH0;
	TL0=TIMER_20MS_TL0;
	EA=0; /* EA和ET0,51上电缺省值为0,EA将在OSStartHighRdy()中打开 */
	ET0=0; /* 满足在OSStart()前不产生时钟中断的要求,系统启动后第一时间开定时器T0中断 */	
	TR0=1;   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -