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

📄 os_cpu_c.c

📁 我移植的uCos 2.52版本,在MCS51上运行,代码进行了一些优化,可以编译在小模式.
💻 C
📖 第 1 页 / 共 2 页
字号:
#pragma SRC
/*
*********************************************************************************************************
*                                               uC/OS-II
*                                         The Real-Time Kernel
*
*                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
*                                          All Rights Reserved
*
*
*                                       80x86/80x88 Specific code
*                                          LARGE MEMORY MODEL
*
* File : OS_CPU_C.C
* By   : Jean J. Labrosse
*
* Ported date:     Dec 2, 2003
* By:              Stuart Wright (swright@jiskoot.com)
* Target platform: Keil C51 V7.07 and above
*         
* Based on port for 8051 by John X. Liu, China, (johnxliu@163.com)
********************************************************************************************************
*/

#define  OS_CPU_GLOBALS	  
#include "includes.h"
#include "os_kcdef.h"
/*
*********************************************************************************************************
*                                        INITIALIZE A TASK'S STACK
*
* Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
*              stack frame of the task being created.  This function is highly processor specific.
*
* Arguments  : task          is a pointer to the task code
*
*              pdata  (vd)   is a pointer to a user supplied data area that will be passed to the task
*                            when the task first executes. 
*
*              ptos          is a pointer to the top of stack.  It is assumed that 'ptos' points to
*                            a 'free' entry on the task stack.  If OS_STK_GROWTH is set to 1 then 
*                            'ptos' will contain the HIGHEST valid address of the stack.  Similarly, if
*                            OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
*                            of the stack.
*
*              opt           specifies options that can be used to alter the behavior of OSTaskStkInit().
*                            (see uCOS_II.H for OS_TASK_OPT_???).
*
* Returns    : Always returns the location of the new top-of-stack' once the processor registers have
*              been placed on the stack in the proper order.
*
* Note(s)    : Interrupts are enabled when your task starts executing. You can change this by setting the
*              PSW to 0x0002 instead.  In this case, interrupts would be disabled upon task startup.  The
*              application code would be responsible for enabling interrupts at the beginning of the task
*              code.  You will need to modify OSTaskIdle() and OSTaskStat() so that they enable 
*              interrupts.  Failure to do this will make your system crash!
*********************************************************************************************************
*/

/* The stack variable points to the start pointer in hardware stack and is defined in OS_CPU_A */
#if OS_STK_GROWTH==0
#define PUSH(A)  *(++stk) = ((INT8U)(A))
#else 
#define PUSH(A)  *(--stk) = ((INT8U)(A))
#endif
extern idata unsigned char STACK_START[1];
//extern idata unsigned char STACK_TOP[1];

OS_STK _XDATA *OSTaskStkInit (void (_CODE *task)(void _XDATA *pd) KCREENTRANT, void _XDATA * pdat, OS_STK _XDATA *ptos, INT16U opt) KCREENTRANT
{
    INT8U _XDATA * stk;
	
	opt+=0;									//opt没被用到
    //opt    = opt;                           /* 'opt' is not used, prevent warning                      */
 	pdat	+=0;
	//pdat   = pdat;
    stk    = (INT8U *) ptos;                //用户堆栈 /* Load stack pointer 
	                                     
	//*(void _XDATA **)stk	 = pdat;                  /*                               */
	//stk				+= sizeof(void _XDATA *);		/* Save the vd to external stack */
	PUSH(task);								/* The value should be loaded to PC    */
	PUSH((INT16U)task>>8);       					/* next time when this task is running */
	
	//PUSH((INT8U )(((INT32U) task>>16)+0x7F));      /* Third byte of PC for dallas 390 */

/* Following is the registers pushed into hardware stack */
	PUSH('A');                 /* ACC */
	PUSH('B');                 /* B   */
	//*++stk			 = 'X';                 /* DPX */
	PUSH('H');                 /* DPH */
	PUSH('L');                 /* DPL */
//	*++stk			 = 'X';                 /* DPX1 for second DPTR */
//	*++stk			 = 'H';                 /* DPH1 for second DPTR */
//	*++stk			 = 'L';                 /* DPL1 for second DPTR */
//	*++stk			 = DPS;                 /* DPS for second DPTR */
	PUSH(PSW);					/* PSW */

	PUSH(0);                   /* R0  */

	PUSH(1);                   // should be R1
	PUSH(2);                   // should be R2
	PUSH(3);                   // should be R3

//	stk				+= sizeof(void *);      /* Keil C uses R1,R2,R3 to pass the */
//	*(void**)stk	 = vd;                  /* arguments of functions.          */

	PUSH(4);                   /* R4  */
	PUSH(5);                   /* R5  */
	PUSH(6);                   /* R6  */
	PUSH(7);                   /* R7  */

/* Following is the registers pushed into hardware stack manually to support the dallas 390 */
    PUSH(0x80);                /* IE, EA is enabled  */
/*
    Next is calculating the hardware stack pointer.
*/	
    PUSH((INT8U) (STACK_START-2      /* Initial value when main was called    */
	                   +1                   /* IE */
	                   +8                   /* R0-R7, eight registers was saved      */
                       +5                   /* ACC, B, DPH, DPL, PSW, five registers */
                       //+1                   /* Dallas 390 extra registers DPX1 */
//                       +4                   /* Dallas 390 extra registers for second DPTR - DPL1 DPH1 DPX1 DPS */
                       +sizeof(INT16U))      /* The PC value to be loaded             */
                       //+sizeof(INT8U)       /* The third byte of PC value to be loaded for dallas 390  */
					   );
	PUSH(0);				   /* C_IBP  *--CIBP */
    return ((void *)stk);
}

OS_EXT  OS_TCB  _IDATA *OSTCBHighRdy;                    /* Pointer to highest priority TCB ready to run  */

extern void LoadCtx() KCREENTRANT;   /* Save the current working registers to stack, defined in OS_CPU_A.ASM */

extern INT8U xdata * data C_XBP;
//extern INT8U * data C_IBP;
/*
***********************************************************************************************************
* OSStartHighRdy: START MULTITASKING
*     
* Duty:     a) Call OSTaskSwHook() then,
*           b) Set OSRunning to TRUE,
*           c) Switch to the highest priority task.
*
***********************************************************************************************************
*/
void OSStartHighRdy(void) KCREENTRANT
{
    OSTaskSwHook();
	OSRunning=1;

	C_XBP=OSTCBHighRdy->OSTCBStkPtr;

	LoadCtx();
}

/**********************************************************************************************************
* C_OSCtxSw is the c part of OSCtxSw.
* When control passes to this function, the processor registers have been saved in external stack
***********************************************************************************************************/
void C_OSCtxSw(void) KCREENTRANT
{
/*	Save processor registers; DONE in the OSCtxSw part in OS_CPU_ASM.ASM                                  */

/*	Save the current task's stack pointer into the current task's OS_TCB:  
		OSTCBCur->OSTCBStkPtr = Stack pointer;
	Call user definable OSTaskSwHook();                                    
	OSTCBCur  = OSTCBHighRdy;                                              
	OSPrioCur = OSPrioHighRdy;                                             
	Get the stack pointer of the task to resume:                           
		Stack pointer = OSTCBHighRdy->OSTCBStkPtr;
	Restore all processor registers from the new task's stack;             
	Execute a return from interrupt instruction;                           */

    OSTCBCur->OSTCBStkPtr = C_XBP;
    
	OSTaskSwHook();
    OSTCBCur  = OSTCBHighRdy;
    OSPrioCur = OSPrioHighRdy;

    C_XBP       = OSTCBCur->OSTCBStkPtr;
    LoadCtx();
}

INT8U data SaveSP;
INT8U * data SaveC_IBP;

void OSIntCtxSw(void) KCREENTRANT
{
	EA=0;
#pragma ASM
	// SP=SaveSP
	// C_IBP=SaveC_IBP;
	MOV SP,SaveSP
	MOV	?C_IBP,SaveC_IBP
#pragma ENDASM

    C_XBP=OSTCBCur->OSTCBStkPtr;

#pragma ASM
	EXTRN   CODE(_?KCOSCtxSw)
	//MOV		A, #BYTE0( _?KCOSCtxSw)
	//PUSH	ACC
	//MOV		A, #BYTE1(_?KCOSCtxSw)
	//PUSH	ACC
	MOV		A, #LOW( _?KCOSCtxSw)
	PUSH	ACC
	MOV		A, #HIGH(_?KCOSCtxSw)
	PUSH	ACC
	//MOV		A, #BYTE2(_?KCOSCtxSw)
	//PUSH	ACC
	RETI
#pragma ENDASM	
}

/* OSTickISR can be writen in c language now, so it is more easy for user to write code for their own */
void OSTickISR(void) interrupt OS_TIME_ISR
{
TH0=(OS_Time_Count)>>8;
TL0+=OS_Time_Count;
/* Do this first */
    OSIntNesting++;                      /* Increment ISR nesting level directly to speed up processing */
//	OSIntEnter();					/* Must be called first at every hardware interrupt entry point */
    if(OSIntNesting==1)                     /* Only at the outerest interrupt we do these. */
		{
#pragma ASM
		PUSH IE
#pragma ENDASM 
		EA=0;
#pragma ASM
		//SaveC_IBP=C_IBP;
		//SaveSP=SP;		
		MOV	SaveC_IBP,?C_IBP	
		MOV SaveSP,SP
#pragma ENDASM

⌨️ 快捷键说明

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