/**********************************************************************/
/*                                                                    */
/* File name: uip_kernel_common.c                                     */
/*                                                                    */
/* Since:     2007-02-25                                              */
/*                                                                    */
/* Version:   uip Kernel v1.09                                        */
/*            Copyright (C) 2006..2018 Wang Baojin.                   */
/*                                                                    */
/* Author:    Designed by Wang Baojin(王保进)                         */
/*                                                                    */
/*                                                                    */
/* Purpose:   uip kernel execution control functions, includeing      */
/*            initialization                                          */
/*                                                                    */
/* Distribution: This file is part of uip kernel.                     */
/*            uip Kernel is free software, you can redistribute it    */
/*            and/or modify it under the terms of the GNU General     */
/*            Public License as published by the Free Software        */
/*            Foundation either version 2, or (at your option)        */
/*            any later version.                                      */
/*                                                                    */
/*            uip Kernel is distributed in the hope that it will be   */
/*            useful, but WITHOUT ANY WARRANTY without even the       */
/*            implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/*            PuipICULAR PURPOSE.  See the GNU General Public         */
/*            License for more details.                               */
/*                                                                    */
/*            You should have received a copy of the GNU General      */
/*            Public License along with gpsim see the file            */
/*            COPYING.txt. If not, write to the Free Software         */
/*            Foundation, 59 Temple Place - Suite 330,                */
/*            Boston, MA 02111-1307, USA.                             */
/*                                                                    */
/*          > A special exception to the GPL can be applied should    */
/*            you wish to distribute a combined work that includes    */
/*            uip Kernel, without being obliged to provide the        */
/*            source code for any proprietary components.             */
/*                                                                    */
/* History:                                                           */
/*   2007/02/25  [WBJ] Create this file.                              */
/*                                                                    */
/**********************************************************************/

#include "uip_kernel_api.h"

/* TCB array defined by user in application */      
/* 标识当前是否为低功耗模式 */
UIP_CHAR                              uipLowPowerMode;

uipTaskPtr                            uipTCBArray;  

UIP_UCHAR                             uipPriorityToTaskID[uipPrioNumber];

UIP_UINT                              uipInitTaskStackPtr[uipPrioNumber];

TaskNumberType                        uipTaskNumber;                               

TaskNumberType                        uipTaskIDNumber;                       

/* Pointer to RCB array defined by user in application */      
uipResPtr                             uipRCBArray;  

ResNumberType                         uipResNumber;                                

/* Array of alarm control block pointer */
uipAlarmPtr                           uipACBArray;

AlarmNumberType                       uipAlarmNumber;                             

AlarmType                             uipAlarmID;

/* Array of message control block pointer */
uipMsgPtr                             uipMCBArray;

MsgNumberType                         uipMsgNumber;                           

uipCounter                            uipSystemCounter;

AlarmBaseType                         uipSystemAlarmBase;

UIP_UCHAR                             uipOSSurport;

UIP_UCHAR                             uipIdleTaskID;

uipTask                               *uipIdleTaskTCBPtr;

UIP_INT32                             uipIntTableID;

UIP_UINT32                            uipInSchedulerLoop;

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    StartOS                                             1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The user can call this system service to start the operating    */
/*    system in a specific mode.                                      */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    Mode                     application mode                       */
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application main function                                       */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID StartOS(AppModeType mode)
{
    uip_INTERRUPT_STATE_SAVED;

    /* Enter critical area */
    uip_KERNEL_DISABLE_INTS;
    
    /* Initialize the uipOS kernel */
    uipKernelInitialize();
    
    uipAppInitialize();

    uipSystemMode = mode;

    /* Start scheduling(never return) and and enable all interrupts */
    uipScheduler();
    
    /* Leave cirtical area */
    uip_KERNEL_RESTORE_INTS;    
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    ShutdownOS                                          1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    This system service can reset system.                           */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    Mode                     application mode                       */
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application, StartOS                                            */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID ShutdownOS(StatusType status)
{
    /* Reset system. Defined by user(for example, reset by watchdog on board) */
    ;
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipTaskInfoInit                                     1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize TCB info used by system.                 */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appTCBArray              TCB array defined by application       */
/*                                                                    */ 
/*    taskNumber               Task number defined by application     */                                                                 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID uipTaskInfoInit(uipTaskPtr appTCBArray, TaskNumberType taskNumber)
{
    /* Initialize task number variable used by system */
    uipTaskNumber = taskNumber;	
    
    /* Set task TCB array pointer used by system */
    uipTCBArray = (uipTaskPtr)appTCBArray;    	
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipResInfoInit                                      1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize RCB info used by system.                 */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appRCBArray              RCB array defined by application       */
/*                                                                    */ 
/*    resNumber                Resource number defined by application */                                                                 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID uipResInfoInit(uipResPtr appRCBArray, ResNumberType resNumber)
{
    /* Initialize task number variable used by system */
    uipResNumber= resNumber;	
    
    /* Set task TCB array pointer used by system */
    uipRCBArray = (uipResPtr)appRCBArray;    	
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipMsgInfoInit                                      1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize Message info used by system.             */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appMCBArray              Message array defined by application   */
/*                                                                    */ 
/*    mcbNumber                Message number defined by application  */                                                                 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID uipMsgInfoInit(uipMsgPtr appMCBArray, MsgNumberType mcbNumber)
{
    /* Initialize task number variable used by system */
    uipMsgNumber = mcbNumber;
    
    /* Set task TCB array pointer used by system */
    uipMCBArray = (uipMsgPtr)appMCBArray;    	
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipAlarmInfoInit                                    1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize alarm info used by system.               */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appACBArray              Alarm array defined by application     */
/*                                                                    */ 
/*    acbNumber                Alarm number defined by application    */                                                                 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID uipAlarmInfoInit(uipAlarmPtr appACBArray, AlarmNumberType acbNumber)
{
    /* Initialize task number variable used by system */
    uipAlarmNumber = acbNumber;
    
    /* Set task TCB array pointer used by system */
    uipACBArray = (uipAlarmPtr)appACBArray;    	
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipKernelInitialize                                 1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize the uipOS kernel component.              */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    None                                                            */                                                                 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    StartOS                                                         */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID uipKernelInitialize(VOID)
{
    UIP_UINT            i, val;
    
    uipTCBArray = uip_NULL;  

    for(i=0; i<uipPrioNumber; i++)
    {
    	uipPriorityToTaskID[i] = uip_NULL;
    	uipInitTaskStackPtr[i] = uip_NULL;
    }

    for(i=0; i<uipGrpMax; i++)
    {
    	uipPrioGrpArray[i] = uip_NULL;
    }
    /* Initialize uipSystemAlarmBase and uipSystemCounter */
    val = sizeof(TickType) - 1;
    uipSystemAlarmBase.maxAllowedValue = 0xFF;
    for(i = 0; i < val; i++)
    {
        /* When change TickType bit, modify this code */
        uipSystemAlarmBase.maxAllowedValue = uipSystemAlarmBase.maxAllowedValue << 8 | (~0x00);  
    }
    uipSystemAlarmBase.ticksPerBase = 1;
    uipSystemAlarmBase.minCycle = 1;
    
    uipSystemCounter.uipCtrBase = &uipSystemAlarmBase;
    uipSystemCounter.uipCtrValue = 0;
    uipSystemCounter.uipTick = 0;        
    
    uipTaskNumber = uip_NULL;                               
    uipTaskIDNumber = uip_NULL;                       
    uipRCBArray = uip_NULL;  
    uipResNumber = uip_NULL;                                
    uipACBArray = uip_NULL;
    uipAlarmNumber = uip_NULL;                             
    uipAlarmID = uip_NULL;
    uipMCBArray = uip_NULL;
    uipMsgNumber = uip_NULL;
    uipLowPowerMode = uip_NULL;                           
    uipSchedulerLock = uip_NULL;
    uipNestedInterrupts = uip_NULL;
    uipSystemMode = uip_NULL;
    uipPrioGroup = uip_NULL;
    uipCurrentTaskTCBPtr = uip_NULL;
    uipNextRunningTaskTCBPtr = uip_NULL;
    uipSystemStackPtr = uip_NULL;
    uipCurrentTaskPriority = uipMinPriority;
    uipNextRunningTaskPriority = uipMinPriority;
    uipInSchedulerLoop = uip_NULL;

    uipIntTableID = uip_NULL;
}

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipTaskStackInit                                    1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize the stack frame of the task.             */
/*    It is processor specific.                                       */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    taskEntry                Pointer to the task code entry         */
/*                                                                    */ 
/*    stackInitialAdress       Ppointer to the top of task's stack    */                                                                 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    Returns the location of the top of new stack                    */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    UIP_TerminateTask()                                             */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
UIP_VOID_PTR uipTaskStackInit(VOID *taskEntry, UIP_UINT stackInitialAdress)
{
    UIP_UINT    *taskStack;

    taskStack = (UIP_UINT *)stackInitialAdress;           /* task's stack pointer                             */

    taskStack--;                  
    *taskStack = (UIP_UINT)0x01000000UL;                  /* ARM Cortex-M0 CPU's xPSR                         */
    taskStack--;                  
    *taskStack = (UIP_UINT)(taskEntry);                   /* ARM Cortex-M0 CPU's PC for task's entry point    */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R14 (LR)                     */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R12                          */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R3                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R2                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R1                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R0                           */
                                                                                                                
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R11                          */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R10                          */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R9                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R8                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R7                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R6                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R5                           */
    taskStack--;                  
    *taskStack = (UIP_UINT)0x00000000L;                   /* ARM Cortex-M0 CPU's R4                           */

    return taskStack;
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipTaskInitialize                                   1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service set the task to the ready state.                    */
/*    May switch to higher priority task if necessary.                */ 
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appTCBArray             Pointer to TCB array defined by user    */ 
/*    tcbNumber               TCB(task) number                        */ 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return                                                            */ 
/*                                                                    */ 
/*    E_OK                     Application initialize completely      */ 
/*    E_OS_VALUE               tcbNumber is over uip_TASK_MAX         */ 
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
StatusType uipTaskInitialize(uipTaskPtr appTCBArray, TaskNumberType tcbNumber)
{
    TaskNumberType        number;
    uipTaskPtr            taskPtr;
    
    /* Check to see if tcbNumber is valid(not larger than 255) */
    if (tcbNumber > uip_TASK_MAX)
    {
         return (E_OS_VALUE); 
    }
    else
    {
        /* Initialize task number variable used by system */
        uipTaskNumber = tcbNumber;
    }	  

    /* Set task TCB array pointer used by system */
    uipTCBArray = (uipTaskPtr)appTCBArray;    	

    /* Save initialization of every task's stack */
    for (number = 0; number < tcbNumber; number++)
    {    	 
    	taskPtr = (uipTaskPtr)((UIP_UINT)uipTCBArray + number*sizeof(uipTask));
    	  
    	uipInitTaskStackPtr[number] = (UIP_UINT)(taskPtr -> uipStackPtr);
        
        uipPriorityToTaskID[taskPtr -> uipReadyPrio] = taskPtr -> uipTaskID;

#ifdef MixPreemption
        uipPriorityToTaskID[taskPtr -> uipDispatchPrio] = taskPtr -> uipTaskID;
#endif
        taskPtr -> uipRdyPrioGrpArrayIndex = (UIP_UCHAR)(taskPtr -> uipReadyPrio >> 3);
        taskPtr -> uipRdyPrioGrpBit = (UIP_UINT)((UIP_UINT)1 << 
                                         (taskPtr -> uipRdyPrioGrpArrayIndex));
        taskPtr -> uipRdyPrioGrpArrayBit = (UIP_UCHAR) ((UIP_UCHAR)1 << 
                                         (taskPtr -> uipReadyPrio & 0x07));
#ifdef MixPreemption
        taskPtr -> uipDispPrioGrpArrayIndex = (UIP_UCHAR)(taskPtr -> uipDispatchPrio >> 3);
        taskPtr -> uipDispPrioGrpBit = (UIP_UINT)((UIP_UINT)1 << 
                                         (taskPtr -> uipDispPrioGrpArrayIndex));
        taskPtr -> uipDispPrioGrpArrayBit = (UIP_UCHAR) ((UIP_UCHAR)1 << 
                                         (taskPtr -> uipDispatchPrio & 0x07));
#endif

        taskPtr -> uipStackPtr = uipTaskStackInit((VOID *)(taskPtr -> uipTaskEntry),
                                                   (UIP_UINT)(taskPtr -> uipStackPtr));
    	taskPtr -> IsInited = 0;
        taskPtr -> UIP_SetEvent = 0x0;
        taskPtr -> uipResources = (uipResPtr)0x0;
         
        /* set the ready bit according to task's current state */
        /* Set ready task's index bit in uipPrioGroup */
        if (((taskPtr -> uipTaskState) & uip_TASK_STATE_MASK) == READY_STATE)
        { 
            uipPrioGroup |= (taskPtr -> uipRdyPrioGrpBit);

            /* Set the ready task's corresponding bit in uipPrioGrpArray[] */
            uipPrioGrpArray[taskPtr -> uipRdyPrioGrpArrayIndex] |= taskPtr -> uipRdyPrioGrpArrayBit;
             /* decide the next running task */
            if(uipNextRunningTaskPriority != uipMinPriority)
            {
                if(taskPtr -> uipReadyPrio < uipNextRunningTaskPriority)
                {
                	uipNextRunningTaskPriority = taskPtr -> uipReadyPrio;
                    uipNextRunningTaskTCBPtr = taskPtr;
                }
            }
            else
            {
                uipNextRunningTaskPriority = taskPtr -> uipReadyPrio;
                uipNextRunningTaskTCBPtr = taskPtr;
            } 
        }        
    }
    
    return (E_OK);    
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipResInitialize                                    1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize all resources' control block(RCB) and    */
/*    corresponding variables.                                        */ 
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appRCBArray             Pointer to RCB array defined by user    */ 
/*    rcbNumber               RCB number                              */ 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return                                                            */ 
/*                                                                    */ 
/*    E_OK                     Application initialize completely      */ 
/*    E_OS_VALUE               rcbNumber is over uip_RES_MAX          */ 
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
StatusType uipResInitialize(uipResPtr appRCBArray, ResNumberType rcbNumber)
{
    ResNumberType        number;
    uipResPtr            resPtr;
    
    /* Check to see if tcbNumber is valid(not larger than 255) */
    if(rcbNumber > uip_RES_MAX)
    {
        return (E_OS_VALUE); 
    }
    else
    {
        /* Initialize resource number variable used by system */
        uipResNumber= rcbNumber;	    	
    }

    /* Set task TCB array pointer used by system */
    uipRCBArray = (uipResPtr)appRCBArray;    	
     
    /* Initialize resources */
    for(number = 0; number < rcbNumber; number++)
    {
    	resPtr = (uipResPtr)((UIP_UINT)uipRCBArray + number*sizeof(uipRes));  
        resPtr -> uipResCeilingGrpArrayIndex = (UIP_UCHAR)(resPtr -> uipCeilingPrio >> 3);
        resPtr -> uipResCeilingGrpBit = (UIP_UINT)((UIP_UINT)1 << 
                                         (resPtr -> uipResCeilingGrpArrayIndex));
        resPtr -> uipResCeilingGrpArrayBit = (UIP_UCHAR) ((UIP_UCHAR)1 << 
                                         (resPtr -> uipCeilingPrio & 0x07));
    }
    
    return (E_OK);    
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipMsgInitialize                                    1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize all messages' control block(MCB) and     */
/*    corresponding variables.                                        */ 
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    appMCBArray             Pointer to MCB array defined by user    */ 
/*    mcbNumber               MCB number                              */ 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return                                                            */ 
/*                                                                    */ 
/*    E_OK                     Application initialize completely      */ 
/*    E_OS_VALUE               mcbNumber is over uip_MSG_MAX          */ 
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
StatusType uipMsgInitialize(uipMsgPtr appMCBArray, MsgNumberType mcbNumber)
{
    MsgNumberType        number;
    uipMsgPtr            msgPtr;
    
    /* Check to see if tcbNumber is valid(not larger than 255) */
    if(mcbNumber > uip_MSG_MAX)
    {
        return (E_OS_VALUE); 
    }
    else
    {
    	/* Initialize task number variable used by system */
    	uipMsgNumber = mcbNumber;	
    }

    /* Set message MCB array pointer used by system */
    uipMCBArray = (uipMsgPtr)appMCBArray;    	
     
    /* Initialization messages */
    for(number = 0; number < uipMsgNumber; number++)
    {
    	msgPtr = (uipMsgPtr)((UIP_UINT)uipMCBArray + number*sizeof(uipMsg));  
        msgPtr -> uipQueueWriteHead = msgPtr -> uipMsgPtr;
        msgPtr -> uipQueueReadHead = msgPtr -> uipMsgPtr;
    }
    
    return (E_OK);    
} /* end */

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipAlarmInitialize                                  1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    The service initialize all alarms' control block(ACB) and       */
/*    corresponding variables.                                        */ 
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    *rcbArray               Pointer to ACB array defined by user    */ 
/*    rcbNumber               ACB number                              */ 
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return                                                            */ 
/*                                                                    */ 
/*    E_OK                     Application initialize completely      */ 
/*    E_OS_VALUE               acbNumber is over uip_ALARM_MAX        */ 
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    Application                                                     */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
StatusType uipAlarmInitialize(uipAlarmPtr appACBArray, AlarmNumberType acbNumber)
{
    ResNumberType        number;
    
    /* Check to see if acbNumber is valid */
    if(acbNumber > uip_ALARM_MAX)
    {
        return (E_OS_VALUE); 
    }
    else
    {
        /* Initialize alarm number variable used by system */
        uipAlarmNumber = acbNumber;	
    }

    /* Set task ACB array pointer used by system */
    uipACBArray = (uipAlarmPtr)appACBArray;    	

    /* Initialize alarms */
    for(number = 0; number < acbNumber; number++)
    {
        /*  */
    	;
    }
    
    return (E_OK);    
} /* end */

/***************************************************************************************/
/*                          End of File : uip_kernel_task.c                            */
/***************************************************************************************/
