/*
*********************************************************************************************************
*                                               uC/OS-II
*                                        The Real-Time Kernel
*
*                            (c) Copyright 2003, Micrium, Inc., Weston, FL
*                                          All Rights Reserved
*
*
* File    : OS_CPU_C.C
* By      : Ahmed Faiz
* Created : 2008/02/12
*********************************************************************************************************
*/

#include "ucos_ii.h"
int int_default_val = 1;


/*
*********************************************************************************************************
*                                             LOCAL FUNCTIONS
*********************************************************************************************************
*/

void    OSTaskCreateHook       (OS_TCB *ptcb);
void    OSTaskDelHook          (OS_TCB *ptcb);
void    OSTaskSwHook           (void);
void    OSTCBInitHook          (OS_TCB *ptcb);
void    OSInitHookBegin        (void);
void    OSInitHookEnd          (void);
void    OSTaskStatHook         (void);
void    OSTimeTickHook         (void);
void    OSTaskIdleHook         (void);
void    OS_CPU_IntHandler      (void);
OS_STK  *OSTaskStkInit         (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT16U opt);


/*
*********************************************************************************************************
*                                          TASK CREATION HOOK
*                                   void  OSTaskCreateHook(OS_TCB *ptcb)
*                                   
* 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.
*
* Returns    : None
*
* Note(s)    : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0 
void  OSTaskCreateHook (OS_TCB *ptcb)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TaskCreateHook(ptcb);
#else
    (void)ptcb;                                         /* Prevent compiler warning                    */
#endif

}
#endif

/*
*********************************************************************************************************
*                                           TASK DELETION HOOK
*                                        void  OSTaskDelHook(OS_TCB *ptcb)
*
* 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.
*
* Returns    : None
*
* Note(s)    : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0 
void  OSTaskDelHook (OS_TCB *ptcb)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TaskDelHook(ptcb);
#else
    (void)ptcb;                                         /* Prevent compiler warning                    */
#endif
}
#endif

/*
*********************************************************************************************************
*                                           TASK SWITCH HOOK
*                                        void  OSTaskSwHook (void)
*
* Description: This function is called when a task switch is performed.  This allows you to perform other
*              operations during a context switch.
*
* Arguments  : None
*
* Returns    : 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).
*********************************************************************************************************
*/

#if (OS_CPU_HOOKS_EN > 0) && (OS_TASK_SW_HOOK_EN > 0)
void  OSTaskSwHook (void)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TaskSwHook();
#endif
}
#endif

/*
*********************************************************************************************************
*                                           OS_TCBInit() HOOK
*
* Description: This function is called by OS_TCBInit() after setting up most of the TCB.
*
* Arguments  : ptcb    is a pointer to the TCB of the task being created.
* 
* Returns    : None
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSTCBInitHook (OS_TCB *ptcb)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TCBInitHook(ptcb);
#else
    (void)ptcb;                                         /* Prevent compiler warning                    */
#endif    
}
#endif


/*
*********************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                      void  OSInitHookBegin (void)                                           
*
* Description: This function is called by OSInit() at the beginning of OSInit().
*
* Arguments  : None
*
* Returns    : None
*
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSInitHookBegin (void)
{
}
#endif

/*
*********************************************************************************************************
*                                       OS INITIALIZATION HOOK (END)
*                                       void  OSInitHookEnd (void)
*
* Description: This function is called by OSInit() at the end of OSInit().
*
* Arguments  : None
*
* Returns    : None
* 
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSInitHookEnd (void)
{
}
#endif

/*
*********************************************************************************************************
*                                           STATISTIC TASK HOOK
*                                        void  OSTaskStatHook (void)
*
* 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
*
* Returns    : None
* 
* Note(s)    : None
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0
void  OSTaskStatHook (void)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TaskStatHook();
#endif
}
#endif

/*
*********************************************************************************************************
*                                               TICK HOOK
*                                       void  OSTimeTickHook (void)
*
* Description: This function is called every tick.
*
* Arguments  : None
*
* Returns    : None
* 
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/

#if (OS_CPU_HOOKS_EN > 0) && (OS_TIME_TICK_HOOK_EN > 0)
void  OSTimeTickHook (void)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TimeTickHook();
#endif
}
#endif

/*
*********************************************************************************************************
*                                               IDLE HOOK
*                                       void  OSTaskIdleHook (void)
*
* Description: This function is called by uC/OS-ii IDLE Task.
*
* Arguments  : None
*
* Returns    : None
*
* Note(s)    : 1) This hook function is called when the IDLE task executes - place code here that
*                 needs to be done when the processor is 'idle' i.e. no tasks are being executed.
*                
*              2) Interrupts should be disabled during this call.
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0 && OS_VERSION >= 251
void  OSTaskIdleHook (void)
{
#if OS_APP_HOOKS_EN > 0
//ducx    App_TaskIdleHook();
#endif    
}
#endif


#if 0
OS_STK  *OSTaskStkInit (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT16U opt)
{
    OS_STK *stk;
    INT8U i;

    opt = opt;                                 /* 'opt' is not used, prevent warning                    */
    stk = ptos;                                /* Load stack pointer                                    */

    *--stk = (OS_STK) 0;           //fp    AGU: [SP + -0x4] = FP;
    *--stk = (OS_STK)  task;               //R31    interruprt  return  pointer  AGU: [SP + -0x8] = R31||
    *--stk = (OS_STK)  0;
    *--stk = (OS_STK)  pdata;    // AGU: [FP + -0x10] = R1
    for (i = 0;i < 25;i++)
    {
        *--stk = (OS_STK)0;
    }
	*--stk = (OS_STK)0;   // AGU:[ISP + -120] = IFP;//R28
	*--stk = (OS_STK)0;   // AGU:[ISP + -124] = R30;//R30
//   *--stk = (OS_STK) __ucps2_OS_CPU_Invalid_Task_Return; //R31    function return  pointer
    *--stk = (OS_STK) task; //R30    function return  pointer
    for (i = 0;i < 3;i++)
    {
        *--stk = (OS_STK)0;
    }
    return ((OS_STK *)stk);  /* Return top-of-stack  */
}
#endif

OS_STK  *OSTaskStkInit (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT16U opt)
{
    OS_STK *stk;
    INT8U i;

    opt = opt;                                 /* 'opt' is not used, prevent warning                    */
    stk = ptos;                                /* Load stack pointer                                    */
	
    for (i = 0;i < 25;i++)
    {
        *--stk = (OS_STK)0;
    }
    *--stk = (OS_STK) 0;           //fp    AGU: [SP + -0x4] = FP;
    *--stk = (OS_STK)  task;               //R31    interruprt  return  pointer  AGU: [SP + -0x8] = R31||
    *--stk = (OS_STK)  0;
    *--stk = (OS_STK)  pdata;    // AGU: [FP + -0x10] = R1
    for (i = 0;i < 25;i++)
    {
        *--stk = (OS_STK)0;
    }
	*--stk = (OS_STK)7;   // AGU:[ISP + -120] = IFP;//R28
	*--stk = (OS_STK)task;   // AGU:[ISP + -124] = R30;//R30
//   *--stk = (OS_STK) __ucps2_OS_CPU_Invalid_Task_Return; //R31    function return  pointer
    *--stk = (OS_STK) task; //R31    function return  pointer
    *--stk = (OS_STK) task;  //r31
    for (i = 0;i < 2;i++)
    {
        *--stk = (OS_STK)0;
    }
    *--stk = (OS_STK)0x3a3b;
    return ((OS_STK *)stk);  /* Return top-of-stack  */
}
/* the original status is Int is enable */


void ucp_enter_critical(void)
{
    if(1 == int_default_val)
    {
        __ucps2_IntEn(f_Disable);
    }

}

void ucp_exit_critical(void)
{
    if(1 == int_default_val)
    {
        __ucps2_IntEn(f_Enable);
    }

}

void ucp_int_enble(void)
{
    int_default_val = 1;
}

void ucp_int_disable(void)
{
    int_default_val=0;
}
