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

📄 os_cpu_c.c

📁 os源代码 os源代码 os源代码 os源代码 os源代码 os源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*********************************************************************************************************
*                                               uC/OS-II
*                                         The Real-Time Kernel
*
*                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
*                                          All Rights Reserved
*
*
*
* File : OS_CPU_C.C
* By   : Jean J. Labrosse
* Modified Apr. 26, 2001 by R.A. Williams, Sandia National Laboratories
*********************************************************************************************************
*/

#define  OS_CPU_GLOBALS
#include "includes.h"

extern  OS_TCB      *OSTCBCur;                        /* Pointer to currently running TCB              */
extern  OS_TCB      *OSTCBFreeList;                   /* Pointer to list of free TCBs                  */
extern  OS_TCB      *OSTCBHighRdy;                    /* Pointer to highest priority TCB ready to run  */
extern  OS_TCB      *OSTCBList;                       /* Pointer to doubly linked list of TCBs         */
extern  OS_TCB      *OSTCBPrioTbl[OS_LOWEST_PRIO + 1];/* Table of pointers to created TCBs             */

extern  INT32U       OSTime;                   /* Current value of system time (in ticks)              */

/*
*********************************************************************************************************
*                                        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         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!
*********************************************************************************************************
*/
INT64U * MyContext;
INT64U Register13;
INT32S ContextSize = CONTEXT_SIZE;
void *OSTaskStkInit (void (*task)(void *pd), void *pdata, void *ptos, INT16U opt)
{
	OS_STK *	StackPtr;  /* This is a copy of the stack pointer. */

	/* 8-byte align task's stack pointer (EABI) */
	StackPtr = (OS_STK *)((OS_STK) ptos & (INT64U) 0xFFFFFFFFFFFFFFF8);  

	/* Leave buffer area for locals "above the stack" */ 
	/* in case the compiler prolog puts variables above the stack */ 
	//StackPtr -= (CONTEXT_SIZE/8);
	MyContext = ptos;
	//SaveContext();
	/* The whole stack frame goes in upside down. */
	*(StackPtr--) = (FP64)31.0;				/* Pretend FP31 */
	*(StackPtr--) = (FP64)30.0;				/* Pretend FP30 */
	*(StackPtr--) = (FP64)29.0;				/* Pretend FP29 */
	*(StackPtr--) = (FP64)28.0;				/* Pretend FP28 */
	*(StackPtr--) = (FP64)27.0;				/* Pretend FP27 */
	*(StackPtr--) = (FP64)26.0;				/* Pretend FP26 */
	*(StackPtr--) = (FP64)25.0;				/* Pretend FP25 */
	*(StackPtr--) = (FP64)24.0;				/* Pretend FP24 */
	*(StackPtr--) = (FP64)23.0;				/* Pretend FP23 */
	*(StackPtr--) = (FP64)22.0;				/* Pretend FP22 */
	*(StackPtr--) = (FP64)21.0;				/* Pretend FP21 */
	*(StackPtr--) = (FP64)20.0;				/* Pretend FP20 */
	*(StackPtr--) = (FP64)19.0;				/* Pretend FP19 */
	*(StackPtr--) = (FP64)18.0;				/* Pretend FP18 */
	*(StackPtr--) = (FP64)17.0;				/* Pretend FP17 */
	*(StackPtr--) = (FP64)16.0;				/* Pretend FP16 */
	*(StackPtr--) = (FP64)15.0;				/* Pretend FP15 */
	*(StackPtr--) = (FP64)14.0;				/* Pretend FP14 */
	*(StackPtr--) = (FP64)13.0;				/* Pretend FP13 */
	*(StackPtr--) = (FP64)12.0;				/* Pretend FP12 */
	*(StackPtr--) = (FP64)11.0;				/* Pretend FP11 */
	*(StackPtr--) = (FP64)10.0;				/* Pretend FP10 */
	*(StackPtr--) = (FP64)9.0;					/* Pretend FP9 */
	*(StackPtr--) = (FP64)8.0;					/* Pretend FP8 */
	*(StackPtr--) = (FP64)7.0;					/* Pretend FP7 */
	*(StackPtr--) = (FP64)6.0;					/* Pretend FP6 */
	*(StackPtr--) = (FP64)5.0;					/* Pretend FP5 */
	*(StackPtr--) = (FP64)4.0;					/* Pretend FP4 */
	*(StackPtr--) = (FP64)3.0;					/* Pretend FP3 */
	*(StackPtr--) = (FP64)2.0;					/* Pretend FP2 */
	*(StackPtr--) = (FP64)1.0;					/* Pretend FP1 */
	*(StackPtr--) = (FP64)0.0;					/* Pretend FP0 */
	*(StackPtr--) = (INT64U)0x000020FC0000;	/* Good FPSCR contents */
	*(StackPtr--) = (INT64U)0x31;			/* Pretend GPR31 */
	*(StackPtr--) = (INT64U)0x30;			/* Pretend GPR30 */
	*(StackPtr--) = (INT64U)0x29;			/* Pretend GPR29 */
	*(StackPtr--) = (INT64U)0x28;			/* Pretend GPR28 */
	*(StackPtr--) = (INT64U)0x27;			/* Pretend GPR27 */
	*(StackPtr--) = (INT64U)0x26;			/* Pretend GPR26 */
	*(StackPtr--) = (INT64U)0x25;			/* Pretend GPR25 */
	*(StackPtr--) = (INT64U)0x24;			/* Pretend GPR24 */
	*(StackPtr--) = (INT64U)0x23;			/* Pretend GPR23 */
	*(StackPtr--) = (INT64U)0x22;			/* Pretend GPR22 */
	*(StackPtr--) = (INT64U)0x21;			/* Pretend GPR21 */
	*(StackPtr--) = (INT64U)0x20;			/* Pretend GPR20 */
	*(StackPtr--) = (INT64U)0x19;			/* Pretend GPR19 */
	*(StackPtr--) = (INT64U)0x18;			/* Pretend GPR18 */
	*(StackPtr--) = (INT64U)0x17;			/* Pretend GPR17 */
	*(StackPtr--) = (INT64U)0x16;			/* Pretend GPR16 */
	*(StackPtr--) = (INT64U)0x15;			/* Pretend GPR15 */
	*(StackPtr--) = (INT64U)0x14;			/* Pretend GPR14 */
	ReportRegister13();
	*(StackPtr--) = (INT64U)Register13;			/* Pretend GPR13 */
	*(StackPtr--) = (INT64U)0x12;			/* Pretend GPR12 */
	*(StackPtr--) = (INT64U)0x11;			/* Pretend GPR11 */
	*(StackPtr--) = (INT64U)0x10;			/* Pretend GPR10 */
	*(StackPtr--) = (INT64U)0x9;				/* Pretend GPR9 */
	*(StackPtr--) = (INT64U)0x8;				/* Pretend GPR8 */
	*(StackPtr--) = (INT64U)0x7;				/* Pretend GPR7 */
	*(StackPtr--) = (INT64U)0x600060006;				/* Pretend GPR6 */
	*(StackPtr--) = (INT64U)0x500050005;				/* Pretend GPR5 */
	*(StackPtr--) = (INT64U)0x400040004;				/* Pretend GPR4 */
	*(StackPtr--) = (INT64U)pdata*0x100000000;				/* Pretend GPR3 */
	*(StackPtr--) = (INT64U)0x2;				/* Pretend GPR2 */
	*(StackPtr--) = (INT64U)0x1;				/* Pretend GPR0 */
	*(StackPtr--) = (INT64U)0x0;				/* Pretend DAR */
	*(StackPtr--) = (INT64U)0x0;				/* Pretend CTX */
	*(StackPtr--) = (INT32U)task*0x100000000;				/* Pretend LR */
	*(StackPtr--) = (INT64U)0x0;				/* Pretend XER */
	*(StackPtr--) = (INT64U)0x0;				/* Pretend CR */
	*(StackPtr--) = (INT64U)(MSR_ALL_EN * 0x100000000);	/* Enable everything, SRR1 */
	*(StackPtr--) = (INT32U)task*0x100000000;			/* SRR0 */
	*(StackPtr--) = (INT64U)(MSR_ALL_EN * 0x100000000);		/* Enable everything, MSR */
	
	return ((void*)(MyContext-(ContextSize/8)));
}

asm void DEC_Hdlr (void)
{
	/* We are already critical at this point.  No need to re-call. */
	li gpr5, 0x1000		/* Re-load decrementer */
	mtdec	gpr5
	li gpr5, 0x0000		/* Clear up the evidence */
	mtmsr gpr5
	blr
}


/*$PAGE*/
#if OS_CPU_HOOKS_EN
/*
*********************************************************************************************************
*                                          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)
{
    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)
{
    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)
{

}

/*
*********************************************************************************************************
*                                           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)
{

}

/*
*********************************************************************************************************
*                                               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)
{

}
#endif



/*******************************************************************************************************/
/* Begin MPC555-Metrowerks CodeWarrior v4 Assembly Functions                                           */
/* R. A. Williams, Sandia National Laboratories																			 */
/* Note: CodeWarrior v4 does not support in-line assembly															 */
/*******************************************************************************************************/

#ifndef GLOBAL_VARS
#define GLOBAL_VARS
INT32U HoldMSR, HoldMSRCS;
INT32S MSRCritical = MSR_CRITICAL;
INT32S MSRAllEn = MSR_ALL_EN;
INT32U TBSCR = 0x25C200;

#endif
/*
*********************************************************************************************************
*                                               ENTER CRITICAL
*
* Description: This function is called to enter critical mode and disable all interrupts.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
asm	void EnterCritical(void)
{
	mtspr	sprg3, gpr4
	mfmsr gpr4
	stw gpr4, HoldMSR				/* Save MSR */
	lis gpr4, MSRCritical@ha
	lwz gpr4, MSRCritical@l(gpr4)
	mtmsr	gpr4
	mfspr gpr4, sprg3
	blr
}

/*
*********************************************************************************************************
*                                               EXIT CRITICAL
*
* Description: This function is called to exit critical mode and disable all interrupts.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
asm	void ExitCritical(void)
{
	mtspr	sprg3, gpr4
	lis gpr4, HoldMSR@ha
	lwz gpr4, HoldMSR@l(gpr4)
	mtmsr	gpr4
	mfspr gpr4, sprg3
	blr
}

/*
*********************************************************************************************************
*                                            START MULTITASKING
*
* Description: This function is called to begin multitasking
*
* Arguments  : none
*
*********************************************************************************************************
*/
void OSStartHighRdy(void)
{
	/* Call OSTaskSwHook	*/
	OSTaskSwHook();					
	/* Get stack pointer of task to resume */
	MyContext = OSTCBCur->OSTCBStkPtr;
	/* Set OSRunning to True */
	OSRunning = TRUE;						
	/* Restore the Context */
	RestoreContext();
	return;
}



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

⌨️ 快捷键说明

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