/*
*********************************************************************************************************
*                                               uC/OS-II
*                                         The Real-Time Kernel
*
*                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
*                                          All Rights Reserved
*
*
*                                          KeilC51 Specific code
*                                          SMALL MEMORY MODEL
*
* File : OS_CPU_C.C
* By   : Jean J. Labrosse
* Refer to Code Written By 		: Yang Yi (http://www.zlgmcu.com/philips/philips-embedsys.asp)
* Port to KeilC51 Small Mode By	: Li Zhanglin (wzzlin@nankai.edu.cn)
*********************************************************************************************************
*/
#define  OS_CPU_GLOBALS
#include "..\uc_os_II\includes.h"

/*
*********************************************************************************************************
*                                          OSTaskStkInit
*
* Description: Init stack before task running.
*
* 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 bottom of stack.
*
* Note(s)    : 1) stack stored as following format

;	CONTENT													START POSITION IN OSTCBStk	
;	----------												----------------------  	
;	AR7									
;	AR6
;	AR5
;	AR4
;	AR3
;	AR2
;	AR1
;	AR0
;	PSW
;	DPL
;	DPH
;	B
;	ACC								
;	HARDWARE STACK CONTENT(NOT INCLUDE REGISTERS)			2+SmltStkSize 						
;	HARDWARE STACK SIZE(INCLUDE REGISTERS)					1+SmltStkSize  		
;	SIMULATED STACK CONTENT									1				
;	?C_IBP													0
			
*********************************************************************************************************
*/

void DT_XDATA *OSTaskStkInit (void (DT_CODE *task)(void DT_XDATA *pd), void DT_XDATA *ppdata, void DT_XDATA *ptos, INT16U opt) REENTRANT
{
    OS_STK DT_XDATA *stk;

    ppdata = ppdata;							
    opt    = opt;                               

    stk    = (OS_STK DT_XDATA *)ptos;           /* bottom of stack				*/
    *stk++ = (0xFF + 1);                        /* C_IBP						*/
												/* simulated stack size == 0	*/
	*stk++ = 2 + 13;							/* tow bytes of return address and 13 byte registers */
    *stk++ = (INT16U)task & 0xFF;               /* low byte of return address	*/
	*stk++ = (INT16U)task >> 8;           	    /* high byte of return address	*/
    *stk++ = 0x0A;                              /* ACC		*/
    *stk++ = 0x0B;                              /* B		*/
    *stk++ = 0xD1;                              /* DPH		*/
    *stk++ = 0xD0;                              /* DPL		*/
	*stk++ = 0x00;                              /* PSW		*/
    *stk++ = 0x00;                              /* R0		*/
    *stk++ = 0x01;                              /* R1		*/
    *stk++ = 0x02;                              /* R2		*/
    *stk++ = 0x03;                              /* R3		*/
    *stk++ = 0x04;                              /* R4		*/
    *stk++ = 0x05;                              /* R5		*/
    *stk++ = 0x06;                              /* R6		*/
    *stk++ = 0x07;                              /* R7		*/
                                                
    return ((void DT_XDATA *)ptos);				/* note return ptos, not stk */
}

/*
*********************************************************************************************************
*                                          OS Time ISR
*
* Description: use T0.
*
* Arguments  : 
*
* Note(s)    : in default, OSTickISR using register bank 0. Register pushing code will added by keilC.
*********************************************************************************************************
*/
void OSTickISR() interrupt 1
{
	TL0 = TIMER_24M_25MS_L;
	TH0 = TIMER_24M_25MS_H;

	OSIntEnter();
	OSTimeTick();
	OSIntExit();
}


/*$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 DT_XDATA *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 DT_XDATA *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
{
}

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

/*
*********************************************************************************************************
*                                               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
{
}
#endif
