/**********************************************************************/
/*                                                                    */
/* File name: uip_kernel_api.h                                        */
/*                                                                    */
/* Since:     2007-02-25                                              */
/*                                                                    */
/* Version:   uip Kernel v1.01                                        */
/*            Copyright (C) 2006..2018 Wang Baojin.                   */
/*                                                                    */
/* Author:    Designed by Wang Baojin(王保进)                         */
/*                                                                    */
/*                                                                    */
/* Purpose:   Definition of all the types, defines and functions      */
/*            used by the uip rtos kernel.                            */
/*                                                                    */
/* 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.                              */
/*                                                                    */
/*   2012/05/16  [WBJ] Added uip_MASK_NONE define                     */
/*                     Added DeclareResource define                   */
/*                     Added DeclareEvent define                      */
/*   2012/05/19  [WBJ] Added register define                          */
/*                                                                    */
/**********************************************************************/

#ifndef uip_KERNEL_API_H_
#define uip_KERNEL_API_H_

#ifdef   __cplusplus

extern   "C" {

#endif

//#define MixPreemption

#define R_1                                          register
#define R_2                                          register
#define R_3                                          register
#define R_4                                          register
#define R_5                                          register

/* Define task states */

#define RUNNING_STATE                                0x0     /* task state running    */
#define WAITING_STATE                                0x2     /* task state waiting    */
#define READY_STATE                                  0x4     /* task state ready      */
#define SUSPENDED_STATE                              0x8     /* task state suspended  */

/* 2012.05.30 for osek os standard */
#define RUNNING                                      RUNNING_STATE     /* task state running    */
#define WAITING                                      WAITING_STATE     /* task state waiting    */
#define READY                                        READY_STATE       /* task state ready      */
#define SUSPENDED                                    SUSPENDED_STATE   /* task state suspended  */

/* All errors of API services */ 
#define E_OK                                         0x00
#define E_OS_ACCESS                                  0x01
#define E_OS_CALLEVEL                                0x02
#define E_OS_ID                                      0x03
#define E_OS_LIMIT                                   0x04
#define E_OS_NOFUNC                                  0x05
#define E_OS_RESOURCE                                0x06
#define E_OS_STATE                                   0x07
#define E_OS_VALUE                                   0x08

#define INVALID_TASK                                 0x10

#define uip_MASK_NONE                                0x00
#define uip_OS_START_ERROR                           0x11

#define uip_COM_LOCKED                               0x20
#define uip_COM_BUFFER                               0x21
#define uip_COM_FLAG                                 0x22
#define uip_COM_BUFFER_EMPTY                         0x23
#define uip_COM_OVERFLOW                             0x24
#define uip_COM_NOTIFICATION_CALLBACK                0x25
#define uip_COM_NOTIFICATION_FLAG                    0x26
#define uip_COM_NOTIFICATION_SETEVENT                0x27
#define uip_COM_NOTIFICATION_ACTIVATETASK            0x28

/* Global defination for low power mode */
/* 与低功耗模式有关的宏定义 */
#define uip_RETURN_FROM_LOW_POWER_MODE               0x5A

/* Private global defination */
#define uip_NESTED_INTERRUPTS                        0x50
#define uip_SCHEDULER_LOCKED                         0x51
#define uip_PREEMPTION_TASK                          0x52

/* Task's property   */
#define uip_TASK_EXTENDED                            0x80    /* Used to check to see if it is a extended task */

/* Task's state mask */
#define uip_TASK_STATE_MASK                          0x0F

#define uip_TRUE                                     0x01
#define uip_FALSE                                    0x00
#define uip_NULL                                     0x00

/* Alarm property */
#define uip_ALARM_OFF                                0x00
#define uip_ALARM_ON                                 0x01

#define uip_MSG_LOCKED                               0x01    /* Message has been locked */
#define uip_MSG_OVERFLOW                             0x02    /* There is message buffer overflow */

#define uip_MSG_UNQUEUED                             0x03    /* Message's type is unqueued */
#define uip_MSG_QUEUED                               0x04    /* Message's type is queued */

#define uipPrioIndexMASKHigh                         0x000000FFUL
#define uipPrioIndexMASKHMiddle                      0x0000FF00UL
#define uipPrioIndexMASKLMiddle                      0x00FF0000UL
#define uipPrioIndexMASKLow                          0xFF000000UL

#define uipPrioNumber                                16
#define uipGrpMax                                    (uipPrioNumber/8)
#define uip_TASK_ID_MAX                              (uipPrioNumber-1)
#define uip_TASK_MAX                                 uip_TASK_ID_MAX

/* The following variable is defined by user */
//#define uipUsedPrioroty                              256                         /* Defines used priority number(0-255)
#define uipUsedPrioroty                              16                         /* Defines used priority number(0-255)
                                                                                  including resources' priority and preemption threshold */                                                               
#define uipSubGrpSize                                ((uipUsedPrioroty)/16 + 1)  /* Size of subgroup  */

#define uipMinPriority                               (uipUsedPrioroty-1)         /* The minimum priority of system */


#define uip_RES_MAX                                  8

#define uip_ALARM_MAX                                8

#define uip_MSG_MAX                                  8

#define TARGET_CPU_REGISTER                          unsigned int
#define uip_INTERRUPT_STATE_SAVED                    TARGET_CPU_REGISTER uipInterruptStateSaved;
#define uip_KERNEL_DISABLE_INTS                      {uipInterruptStateSaved = uipDisableAllInterrupts();}
#define uip_KERNEL_RESTORE_INTS                      {uipIntMaskStateRestore(uipInterruptStateSaved);}

#define  UIP_CORTEX_M_NVIC_INT_CTRL_REG              *((TARGET_CPU_REGISTER *)0xE000ED04)
#define  UIP_CORTEX_M_NVIC_PENDSVSET_VAL             0x10000000
                         
#define uipEnterScheduler()                          UIP_CORTEX_M_NVIC_INT_CTRL_REG = UIP_CORTEX_M_NVIC_PENDSVSET_VAL
#define uipScheduler()                               UIP_CORTEX_M_NVIC_INT_CTRL_REG = UIP_CORTEX_M_NVIC_PENDSVSET_VAL

/* Define the size of TCB structure */
#define uip_TCB_SIZE                                 sizeof(uipTask)

/* Define the size of RCB structure */
#define uip_RCB_SIZE                                 sizeof(uipRes)

/* Maximum number that the task can be activated */
#define ACTIVATED_NUM_MAX                            16

/* 中断嵌套层级的上限 */
/* Maximum number of nested interrupts */
#define uip_INT_NESTED_MAX                           (100)

/* UIP Kernel 表征调度内核是否已经开始执行的宏定义 */
#define uip_KERNEL_RUNNING                           1

/* Define specific types for target processor */ 

typedef signed char                 UIP_CHAR;
typedef unsigned char               UIP_UCHAR;
typedef signed char                 UIP_INT8;
typedef unsigned char               UIP_UINT8;
typedef signed short                UIP_INT16;
typedef unsigned short              UIP_UINT16;
typedef signed int                  UIP_INT;
typedef unsigned int                UIP_UINT;
typedef signed long                 UIP_INT32;
typedef unsigned long               UIP_UINT32;
#define VOID                        void

typedef signed char *               UIP_CHAR_PTR;
typedef unsigned char *             UIP_UCHAR_PTR;
typedef signed char *               UIP_INT8_PTR;
typedef unsigned char *             UIP_UINT8_PTR;
typedef signed short *              UIP_INT16_PTR;
typedef unsigned short *            UIP_UINT16_PTR;
typedef signed int *                UIP_INT_PTR;
typedef unsigned int *              UIP_UINT_PTR;
typedef signed long *               UIP_INT32_PTR;
typedef unsigned long *             UIP_UINT32_PTR;
typedef void *                      UIP_VOID_PTR;
/* 2013.04.23 修改TaskType的类型值，以便进行越界判断时能起作用 */
typedef UIP_INT32                   TaskType;
typedef UIP_INT32_PTR               TaskRefType;
typedef UIP_UCHAR                   TaskStateType;
typedef UIP_UCHAR_PTR               TaskStateRefType;
typedef UIP_UCHAR                   TaskNumberType;                /* The variable type for task number */        

typedef UIP_UCHAR                   StatusType;
typedef UIP_UINT                    PointerType;                   /* The variable type for pointer used by system */
//typedef UIP_UINT                    TARGET_CPU_REGISTER;           /* The size of target CPU status register */
typedef UIP_UCHAR *                 AppDataRef;
typedef UIP_UCHAR                   FlagType;
typedef UIP_UCHAR                   AppModeType;                   /* Application mode type */

typedef UIP_UINT                    ResourceType;
typedef UIP_UCHAR                   ResNumberType;                 /* The variable type for resource number */        


typedef UIP_UINT                    EventMaskType;
typedef EventMaskType *             EventMaskRefType;

typedef UIP_UCHAR                   AlarmType;
typedef UIP_UCHAR_PTR               AlarmRefType;
typedef UIP_UINT                    TickType;
typedef UIP_UINT_PTR                TickRefType;
typedef UIP_UCHAR                   AlarmNumberType;

typedef UIP_UCHAR                   MsgType;
typedef UIP_UCHAR                   MsgNumberType;                          

/* Define resource control block structure */
typedef struct uip_RESOURCE_STRUCT 
{
    struct uip_TSK_STRUCT    
                      *uipTaskPtr;                     /* Pointer to TCB of task that is using this resource */
    struct uip_RESOURCE_STRUCT    
                      *uipNextRes;                     /* Link to next resource */
    UIP_UINT          uipResCeilingGrpBit;             /* Bit value in task priority group
                                                           corresponding to task ready priority */
    UIP_UCHAR         uipCeilingPrio;                  /* Resource's mutex ceiling priority */
    UIP_UCHAR         uipResCeilingGrpArrayIndex;      /* Index value into ready task table 
                                                           corresponding to task ready priority */
    UIP_UCHAR         uipResCeilingGrpArrayBit;        /* Bit position in ready table 
                                                           corresponding to task ready priority */
    UIP_UCHAR         uipTaskPrio;                     /* Dispatched priority of task that is using this resource */
    UIP_UCHAR         uipLock;                         /* Show the state of resource: 
                                                                           0 not being used; 
                                                                           1 being used      */
    UIP_UCHAR         padding_1;                       /* Padding byte for ARM */
    UIP_UCHAR         padding_2;                       /* Padding byte for ARM */
    UIP_UCHAR         padding_3;                       /* Padding byte for ARM */
} uipRes; 

typedef uipRes *                       uipResPtr;

/* Define global variable for managing task schedule */
/* Define task control block structure. */
typedef struct uip_TSK_STRUCT
{
    UIP_UCHAR         uipTaskID;                       /* Task's ID */
    UIP_UCHAR         uipTaskState;                    /* Taks's execution state */
    UIP_UCHAR         uipReadyPrio;                    /* Task's ready priority */
#ifdef MixPreemption
    UIP_UCHAR         uipDispatchPrio;                 /* Task's dispatched priority */
#else
    UIP_UCHAR         padding_1;                       /* Padding byte for ARM */    
#endif
    struct uip_RESOURCE_STRUCT  
                      *uipResources;                   /* The occupaited resources list of this task*/
    VOID              (*uipTaskEntry)(VOID);           /* Task's entry point */
    UIP_VOID_PTR      uipStackPtr;                     /* Task's stack starting or end address */
    UIP_UINT          uipStackSize;                    /* Task's stack size */
    EventMaskType     UIP_WaitEvent;                   /* Mask of waited events */
    EventMaskType     UIP_SetEvent;                    /* Mask of setted events */
    UIP_UINT          uipRdyPrioGrpBit;                /* Bit value in task priority group
                                                           corresponding to task ready priority */
    UIP_UCHAR         uipRdyPrioGrpArrayIndex;         /* Index value into ready task table 
                                                           corresponding to task ready priority */
    UIP_UCHAR         uipRdyPrioGrpArrayBit;           /* Bit position in ready table 
                                                           corresponding to task ready priority */
    UIP_UCHAR         uipActivatedNumber;              /* Task activated number */
    UIP_UCHAR         padding_2;                       /* Padding byte for ARM */    
#ifdef MixPreemption
    UIP_UINT          uipDispPrioGrpBit;               /* Bit value in task priority group
                                                           corresponding to task dispatch priority */
    UIP_UCHAR         uipDispPrioGrpArrayIndex;        /* Index value into ready task table 
                                                           corresponding to task dispatch priority */
    UIP_UCHAR         uipDispPrioGrpArrayBit;          /* Bit position in ready table 
                                                           corresponding to task dispatch priority */
    UIP_UCHAR         padding_3;                       /* Padding byte for ARM */    
    UIP_UCHAR         padding_4;                       /* Padding byte for ARM */    
#endif
    /* 2013.03 添加 */
    UIP_UINT          IsInited;
} uipTask; 

typedef uipTask *                      uipTaskPtr;

typedef struct uip_ALARMBASE_STRUCT
{
    TickType          maxAllowedValue;                 /* Maximum possible allowed count 
                                                           value in ticks  */
    TickType          ticksPerBase;                    /* Number of ticks required to reach 
                                                           a counter-specific (significant) unit */
    TickType          minCycle;                        /* Smallest allowed value for the 
                                                           cycle-parameter of SetRelAlarm/SetAbsAlarm) 
                                                           (only for systems with extended status) */
} AlarmBaseType; 

typedef AlarmBaseType *                AlarmBaseRefType;

/* Counter struct */
typedef struct uip_CTR_STRUCT
{
    TickType          uipCtrValue;                      /* Counter's current value */
    TickType          uipTick;
    struct uip_ALARMBASE_STRUCT  
                      *uipCtrBase;
} uipCounter; 

typedef uipCounter *                   uipCounterPtr;

/* Alarm struct */
typedef struct uip_ALARM_STRUCT
{
    TickType          uipAlarmValue;    
    UIP_UINT          uipCycle;         
    UIP_UINT          uipEvent;
    struct uip_CTR_STRUCT    
                      *uipCounterPtr;       
    VOID              (*uipCallBack)(VOID);
    UIP_UCHAR         uipAlarmActTaskID;
    UIP_UCHAR         uipAlarmState;            
    UIP_UCHAR         padding_1;                       /* Padding byte for ARM */    
    UIP_UCHAR         padding_2;                       /* Padding byte for ARM */    
} uipAlarm;

typedef uipAlarm *                     uipAlarmPtr;

/* Define message control block structure */
typedef struct uip_MSG_STRUCT
{
    UIP_UCHAR         uipMsgId;                         /* Message's id */
    UIP_UCHAR         uipMsgStatus;                     /* Messages's current state(locked, unlocked, 
                                                           overflow or not etc.) */
    UIP_UCHAR         uipMsgType;                       /* Messages's type(unqueued or queued message) */
    UIP_UCHAR         uipMsgFlag;                       /* Flags to be set for notifying */
    UIP_UCHAR_PTR     uipMsgPtr;                        /* Point to buffer start address */ 
    UIP_UCHAR_PTR     uipQueueWriteHead;                /* Point to buffer address to be writen */ 
    UIP_UCHAR_PTR     uipQueueEnd;                      /* Point to buffer end address */ 
    UIP_UCHAR_PTR     uipQueueReadHead;                 /* Point to buffer address to be read */
    UIP_UINT          uipMsgSize;                       /* Size of messages(bytes) */
    UIP_UINT          uipMsgEvent;                      /* Event to be set for notifying */
    VOID              (*uipMsgCallBack)(VOID);          /* Callback routine called when sending message */
    UIP_UCHAR         uipMsgNotifyTaskID;               /* The ID of task to be nofified */
    UIP_UCHAR         uipMsgNotifyType;                 /* Notification type */    
    UIP_UCHAR         padding_2;                        /* Padding byte for ARM */    
    UIP_UCHAR         padding_3;                        /* Padding byte for ARM */    
} uipMsg; 
 
typedef uipMsg *                       uipMsgPtr;

/* Define int control block structure */
/* 定义中断处理控制结构体 */
typedef struct uip_INT_STRUCT
{
    UIP_UINT32        uipIntId;                         /* Int's id */
    UIP_UINT32        uipIntExcepted;                   /* Expected outcome of int handling. 期望的中断处理结果 */
    UIP_UINT32        uipIntOutcome;                    /* The outcome of int handling. 实际的中断处理结果 */
    VOID              *uipInfo;                         /* Info for Int's callback. 指向回调函数使用的各种数据 */
    UIP_UINT32        (*uipIntCallBack)(struct uip_INT_STRUCT *uipIntContorl); /* Callback routine called when handling int. */
                                                        /* 在中断处理程序最后执行的回调函数，可执行各种判断结果，从而确定退出时处理机制的操作，
                                                           通过返回值告知上一层调用逻辑，执行何种退出处理机制 */
    VOID              *uipIntReserved;                  /* reserved pointer for more information. 预留的指针，用于传递更多的参数 */
} uipInt; 

typedef uipInt *                       uipIntPtr;

typedef UIP_UINT32 (*uipIntCallBackFunction)(struct uip_INT_STRUCT *uipIntContorl);

/************************************ private functions defination ************************************/
/**********************************************************************/
/*    The system service UIP_GetAlarmBase reads the alarm base         */
/*    characteristics. The return value <Info> is a structure         */
/*    in which the information of data type AlarmBaseType is stored.  */ 
/**********************************************************************/
StatusType      UIP_GetAlarmBase(AlarmType AlarmID, AlarmBaseRefType Info);

/**********************************************************************/
/*    The system service returns the relative value in ticks before   */
/*    the alarm<AlarmID> expires.                                     */ 
/**********************************************************************/
StatusType      UIP_GetAlarm(AlarmType AlarmID, TickRefType Tick);

/**********************************************************************/
/*    The system service occupies the alarm <AlarmID> element.        */
/*    After <increment> ticks have elapsed, the task assigned         */
/*    to the alarm <AlarmID> is activated or the assigned event       */
/*    (only for extended tasks) is set or the alarm-callback          */
/*    routine is called.                                              */ 
/**********************************************************************/
StatusType      UIP_SetRelAlarm(AlarmType AlarmID, TickType increment, TickType cycle);

/**********************************************************************/
/*    The system service occupies the alarm <AlarmID> element.        */
/*    When <start> ticks are reached, the task assigned to the alarm  */
/*    <AlarmID> is activated or the assigned event (only for extended */
/*    tasks) is set or the alarm-callback routine is called.          */
/**********************************************************************/
StatusType      UIP_SetAbsAlarm(AlarmType AlarmID, TickType start, TickType cycle);

/**********************************************************************/
/*    The system service cancels the alarm <AlarmID>.                 */ 
/**********************************************************************/
StatusType      UIP_CancelAlarm(AlarmType AlarmID);

/**********************************************************************/
/*    System counter(system timer)'s tick handler                     */ 
/**********************************************************************/
VOID            uipSystemCounterHandler(VOID);

/**********************************************************************/
/*    AlarmID's handler                                               */ 
/**********************************************************************/
VOID            uipAlarmHandler(UIP_UINT alarm_index);

/**********************************************************************/
/*    General counter's tick handler                                  */ 
/**********************************************************************/
VOID            uipGeneralCounterHandler(AlarmType AlarmID);

/**********************************************************************/
/*    hard fault handler                                              */
/**********************************************************************/
VOID            HardFault_Handler(VOID);
VOID            UsageFault_Handler(VOID);
/**********************************************************************/
/*    Get counter's current value                                     */ 
/**********************************************************************/
StatusType      uipGetCtrCurrentValue(AlarmType AlarmID, TickRefType Tick);

#ifdef  SYSTEM_INIT
#define SYSTEM_DECLARE 
#else
#define SYSTEM_DECLARE extern
#endif

/* 标识当前是否为低功耗模式 */
SYSTEM_DECLARE UIP_CHAR                              uipLowPowerMode;

/* Locked number of scheduler(disable preempt)  */
SYSTEM_DECLARE UIP_CHAR                              uipSchedulerLock; 
 
/* Interrupt nested number of system  */
SYSTEM_DECLARE UIP_UINT                              uipNestedInterrupts;

SYSTEM_DECLARE AppModeType                           uipSystemMode;

/* uipPrioGroup is a 32 bit unsigned long integer. Each bit corresponds to 
   eight degressive priority values. These eight prioriyt values is made up 
   of a priority group   */
SYSTEM_DECLARE UIP_UINT                              uipPrioGroup;

/* uipPrioGrpArray is an array of priority groups. Index 31 corresponds to priorities 
   247-255. Bit 0 of this element corresponds priority 247, and bit 7 corresponds 
   priority 255.  */
SYSTEM_DECLARE UIP_UCHAR                             uipPrioGrpArray[uipGrpMax];

/* TCD_Lowest_Set_Bit are lookup tables that is used to determine the highest priority task.  */
SYSTEM_DECLARE UIP_UCHAR                             uipLowHalfByteValue[]; // = {0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};

SYSTEM_DECLARE UIP_UCHAR                             uipHighHalfByteValue[];  // = {0, 4, 5, 4, 6, 4, 5, 4, 7, 4, 5, 4, 6, 4, 5, 4};

/* Pointer to currently running task's TCB */
SYSTEM_DECLARE uipTask                               *uipCurrentTaskTCBPtr;

/* Pointer to next running task's TCB. This task has the highest
   priority in ready tasks */
SYSTEM_DECLARE uipTask                               *uipNextRunningTaskTCBPtr;

SYSTEM_DECLARE VOID                                  *uipSystemStackPtr; 

/* Priority of current task */
SYSTEM_DECLARE UIP_UCHAR                             uipCurrentTaskPriority;                

/* Priority of next running task  */
SYSTEM_DECLARE UIP_UCHAR                             uipNextRunningTaskPriority;           

/* alarm中对非应用TASK的标示 */
SYSTEM_DECLARE UIP_UCHAR                             uipIdleTaskID;

SYSTEM_DECLARE uipTask                               *uipIdleTaskTCBPtr;

SYSTEM_DECLARE VOID                                  uipArmIntHandlerGate(void);

SYSTEM_DECLARE UIP_INT32                             uipIntTableID;

SYSTEM_DECLARE UIP_UINT32                            uipInSchedulerLoop;

#ifdef MixPreemption
/* Reset corresponding bit of some task in priority group and priority group array  */
VOID                           uipResetPrioGrpAndArrayBit(VOID);
#endif

/* Clear corresponding bit of some task in priority group and priority group array  */
VOID                           uipClearPrioGrpAndArrayBit(uipTaskPtr suspendTaskPtr);

/* Caculate and get next running task */
SYSTEM_DECLARE VOID            uipGetNextTask(VOID);

/* Caculate and get next running task's priority */
UIP_UCHAR                      uipGetNextPriority(VOID);
 
/* Reset corresponding bit for priority management */
VOID                           uipClearPrioGrpAndArrayBit(uipTaskPtr suspendTaskPtr);

/* System scheduler's functions: enter scheduler */
/* VOID                           uipEnterScheduler(VOID); */

/* SYSTEM_DECLARE VOID            uipScheduler(VOID);      */

TARGET_CPU_REGISTER            uipDisableAllInterrupts(VOID);   
        
VOID                           uipIntMaskStateRestore(TARGET_CPU_REGISTER cpuStateRegister);


/**********************************************************************/
/*    This call serves to enter critical sections in the code that    */
/*    are assigned to the resource referenced by <ResID>. A critical  */
/*    section shall always be left using UIP_ReleaseResource.         */ 
/**********************************************************************/
StatusType      UIP_GetResource(ResourceType ID);

/**********************************************************************/
/*    UIP_ReleaseResource is the counterpuip of UIP_GetResource and   */
/*    serves to leave critical sections in the code that are assigned */
/*    to the resource referenced by <ResID>.                          */ 
/**********************************************************************/
StatusType      UIP_ReleaseResource(ResourceType ID);


/**********************************************************************/
/*    The service updates the message object identified by <Message>  */
/*    with the application message referenced by the<DataRef>         */
/*    parameter.                                                      */ 
/**********************************************************************/
StatusType      UIP_SendMessage(MsgType MsgID, AppDataRef DataRef);

/**********************************************************************/
/*    The service updates the application message referenced by       */
/*    <DataRef> with the data in the message object identified by     */
/*    <Message>. It resets all flags (Notification classes 1 and 3)   */
/*    associated with <Message>.                                      */ 
/**********************************************************************/
StatusType      UIP_ReceiveMessage(MsgType MsgID, AppDataRef DataRef);

/**********************************************************************/
/*    The service returns the current status of the message object    */
/*    <Message>.                                                      */ 
/**********************************************************************/
StatusType      UIP_GetMessageStatus(MsgType MsgID);

/**********************************************************************/
/*    This service returns COM_TRUE if <Flag> is set, otherwise it    */
/*    returns COM_FALSE.                                              */ 
/**********************************************************************/
FlagType        UIP_ReadFlag(MsgType MsgID);

/**********************************************************************/
/*    This service resets <Flag>                                      */ 
/**********************************************************************/
StatusType      UIP_ResetFlag(MsgType MsgID);

/**********************************************************************/
/*    The service starts and initialises the OSEK COM implementation  */
/*    in the requested application mode.                              */ 
/**********************************************************************/
StatusType      UIP_StartCOM(void);

/**********************************************************************/
/*    The service close OSEK COM                                      */ 
/**********************************************************************/
StatusType      UIP_CloseCOM(void);

/**********************************************************************/
/*    The service UIP_StopCOM causes all OSEK COM activity to cease    */
/*    immediately. All resources used by OSEK COM are returned or     */
/*    left in an inactive state. Data loss is possible.               */ 
/**********************************************************************/
StatusType      UIP_StopCOM(UIP_UCHAR ShutdownMode);


#ifdef  EXEC_INIT
#define EXEC_DECLARE 
#else
#define EXEC_DECLARE extern
#endif

/* TCB array defined by user in application */      
EXEC_DECLARE uipTaskPtr                            uipTCBArray;  

EXEC_DECLARE UIP_UCHAR                             uipPriorityToTaskID[uipPrioNumber];

EXEC_DECLARE UIP_UINT                              uipInitTaskStackPtr[uipPrioNumber];

EXEC_DECLARE TaskNumberType                        uipTaskNumber;                               

EXEC_DECLARE TaskNumberType                        uipTaskIDNumber;                       

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

EXEC_DECLARE ResNumberType                         uipResNumber;                                

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

EXEC_DECLARE AlarmNumberType                       uipAlarmNumber;                             

EXEC_DECLARE AlarmType                             uipAlarmID;

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

EXEC_DECLARE MsgNumberType                         uipMsgNumber;                           

EXEC_DECLARE uipCounter                            uipSystemCounter;

/* 增加 uipSystemCounter 使用的 AlarmBaseType */
EXEC_DECLARE AlarmBaseType                         uipSystemAlarmBase;

EXEC_DECLARE StatusType                            uipCOMInit(VOID);
/**********************************************************************/
/*    The user can call this system service to start the operating    */
/*    system in a specific mode.                                      */
/**********************************************************************/
VOID            StartOS(AppModeType mode);

/**********************************************************************/
/*    This system service can reset system.                           */
/**********************************************************************/
VOID            ShutdownOS(StatusType status);

/**********************************************************************/
/*    The service initialize TCB info used by system.                 */
/**********************************************************************/
VOID            uipTaskInfoInit(uipTaskPtr appTCBArray, TaskNumberType taskNumber);

/**********************************************************************/
/*    The service initialize RCB info used by system.                 */
/**********************************************************************/
VOID            uipResInfoInit(uipResPtr appRCBArray, ResNumberType resNumber);

/**********************************************************************/
/*    The service initialize Message info used by system.             */
/**********************************************************************/
VOID            uipMsgInfoInit(uipMsgPtr appMCBArray, MsgNumberType mcbNumber);

/**********************************************************************/
/*    The service initialize alarm info used by system.               */
/**********************************************************************/
VOID            uipAlarmInfoInit(uipAlarmPtr appACBArray, AlarmNumberType acbNumber);

/**********************************************************************/
/*    This function initialize the stack frame of the task.           */
/*    It is processor specific.                                       */
/**********************************************************************/
UIP_VOID_PTR    uipTaskStackInit(void *taskEntry, UIP_UINT stackInitialAdress);

/**********************************************************************/
/*    The service set the task to the ready state.                    */
/*    May switch to higher priority task if necessary.                */ 
/**********************************************************************/
StatusType      uipTaskInitialize(uipTaskPtr appTCBArray, TaskNumberType tcbNumber);

VOID            uipKernelInitialize(VOID);

/**********************************************************************/
/*    The service initialize all resources' control block(RCB) and    */
/*    corresponding variables.                                        */ 
/**********************************************************************/
StatusType      uipResInitialize(uipResPtr appRCBArray, ResNumberType rcbNumber);

/**********************************************************************/
/*    The service initialize all messages' control block(MCB) and     */
/*    corresponding variables.                                        */ 
/**********************************************************************/
StatusType      uipMsgInitialize(uipMsgPtr appMCBArray, MsgNumberType mcbNumber);

/**********************************************************************/
/*    The service initialize all alarms' control block(ACB) and       */
/*    corresponding variables.                                        */ 
/**********************************************************************/
StatusType      uipAlarmInitialize(uipAlarmPtr appACBArray, AlarmNumberType acbNumber);


/**********************************************************************/
/*    The service called by user application, and used to initialize  */
/*    all RTOS resources(not OSEK' resource)                          */ 
/**********************************************************************/
VOID            uipAppInitialize(VOID);


/**********************************************************************/
/*    The events of task <TaskID> are set according to the event      */
/*    mask <Mask>. Calling UIP_SetEvent causes the task <TaskID> to    */
/*    be transferred to the ready state, if it was waiting for at     */
/*    least one of the events specified in <Mask>.                    */ 
/**********************************************************************/
StatusType      UIP_SetEvent(TaskType TaskID, EventMaskType Mask);

/**********************************************************************/
/*    The events of the extended task calling UIP_ClearEvent are       */
/*     cleared according to the event mask <Mask>.                    */ 
/**********************************************************************/
StatusType      UIP_ClearEvent(EventMaskType Mask);

/**********************************************************************/
/*    This service returns the current state of all event bits of     */
/*    the task <TaskID>, not the events that the task is waiting for  */
/*    The current status of the event mask of task <TaskID> is copied */
/*    to <Event>                                                      */ 
/**********************************************************************/
StatusType      UIP_GetEvent(TaskType TaskID, EventMaskRefType Mask);

/**********************************************************************/
/*    The state of the calling task is set to waiting, unless at      */
/*    least one of the events specified in <Mask> has already been    */
/*    set.                                                            */ 
/**********************************************************************/
StatusType      UIP_WaitEvent(EventMaskType Mask);


/**********************************************************************/
/*    The task <TaskID> is transferred from the suspended state       */
/*    into the ready state14. The operating system ensures that       */
/*    the task code is being executed from the first statement.       */ 
/**********************************************************************/
StatusType      UIP_ActivateTask(TaskType TaskID);

/**********************************************************************/
/*    This service causes the termination of the calling task.        */
/*    The calling task is transferred from the running state          */
/*    into the suspended state.                                       */
/**********************************************************************/
StatusType      UIP_TerminateTask(VOID);

/**********************************************************************/
/*    This service causes the termination of the calling task.        */
/*    After termination of the calling task a succeeding task         */
/*    <TaskID> is activated. Using this service, it ensures that      */
/*    the succeeding task starts to run at the earliest after         */
/*    the calling task has been terminated.                           */ 
/**********************************************************************/
StatusType      UIP_ChainTask(TaskType TaskID);

/**********************************************************************/
/*    If a higher-priority task is ready, the internal resource       */
/*    of the task is released, the current task is put into the       */
/*    ready state, its context is saved and the higher-priority       */
/*    task is executed. Otherwise the calling task is continued.      */ 
/**********************************************************************/
StatusType      UIP_Schedule(VOID); 

/**********************************************************************/
/*    Returns the information about the TaskID of the task which      */
/*    is currently running.                                           */ 
/**********************************************************************/
StatusType      UIP_GetTaskID(TaskRefType TaskID);

/**********************************************************************/
/*    Returns the state of a task (running, ready, waiting,           */
/*    suspended).                                                     */ 
/**********************************************************************/
StatusType      UIP_GetTaskState(TaskType TaskID, TaskStateRefType State); 

VOID            uipIRQIntContextSave(VOID);
VOID            uipSystemCounterHandler(VOID);
VOID            uipIRQIntContextRestore(VOID);
VOID            HardFault_Handler(VOID);

VOID            uipPreContexSwitch(VOID);
VOID            uipPendSVHandler(VOID);
VOID            uipInterruptServiceEngine(VOID);
VOID            uipInterruptHandlingCallback(VOID);
/* VOID            uipContextSwitch(VOID); */

/************************************ private functions defination ************************************/

#define         TASK(Name)                VOID Name(VOID)
#define         DeclareTask(Task)	      extern TASK(Task)
#define         DeclareResource(ResName)  extern ResourceType ResName
#define         DeclareEvent(EventName)	  extern EventMaskType EventName

#define         ActivateTask              UIP_ActivateTask
#define         TerminateTask             UIP_TerminateTask
#define         ChainTask                 UIP_ChainTask
#define         Schedule                  UIP_Schedule 
#define         GetTaskID                 UIP_GetTaskID
#define         GetTaskState              UIP_GetTaskState 

#define         GetAlarmBase              UIP_GetAlarmBase
#define         GetAlarm                  UIP_GetAlarm
#define         SetRelAlarm               UIP_SetRelAlarm
#define         SetAbsAlarm               UIP_SetAbsAlarm
#define         CancelAlarm               UIP_CancelAlarm

#define         SetEvent                  UIP_SetEvent
#define         ClearEvent                UIP_ClearEvent
#define         GetEvent                  UIP_GetEvent
#define         WaitEvent                 UIP_WaitEvent

#define         SendMessage               UIP_SendMessage
#define         ReceiveMessage            UIP_ReceiveMessage
#define         GetMessageStatus          UIP_GetMessageStatus
#define         ReadFlag                  UIP_ReadFlag
#define         ResetFlag                 UIP_ResetFlag
#define         StartCOM                  UIP_StartCOM
#define         CloseCOM                  UIP_CloseCOM
#define         StopCOM                   UIP_StopCOM

#define         GetResource               UIP_GetResource
#define         ReleaseResource           UIP_ReleaseResource

#define         DMA_INT_HANDLING_DATA_LENGTH  4

/* Define the system API's function prototypes. */

StatusType      ActivateTask(TaskType TaskID);
StatusType      TerminateTask(VOID);
StatusType      ChainTask(TaskType TaskID);
StatusType      Schedule(VOID); 
StatusType      GetTaskID(TaskRefType TaskID);
StatusType      GetTaskState(TaskType TaskID, TaskStateRefType State); 

StatusType      GetAlarmBase(AlarmType AlarmID, AlarmBaseRefType Info);
StatusType      GetAlarm(AlarmType AlarmID, TickRefType Tick);
StatusType      SetRelAlarm(AlarmType AlarmID, TickType increment, TickType cycle);
StatusType      SetAbsAlarm(AlarmType AlarmID, TickType start, TickType cycle);
StatusType      CancelAlarm(AlarmType AlarmID);

StatusType      SetEvent(TaskType TaskID, EventMaskType Mask);
StatusType      ClearEvent(EventMaskType Mask);
StatusType      GetEvent(TaskType TaskID, EventMaskRefType Mask);
StatusType      WaitEvent(EventMaskType Mask);

StatusType      SendMessage(MsgType MsgID, AppDataRef DataRef);
StatusType      ReceiveMessage(MsgType MsgID, AppDataRef DataRef);
StatusType      GetMessageStatus(MsgType MsgID);
FlagType        ReadFlag(MsgType MsgID);
StatusType      ResetFlag(MsgType MsgID);
StatusType      StartCOM(VOID);
StatusType      CloseCOM(VOID);
StatusType      StopCOM(UIP_UCHAR ShutdownMode);

StatusType      GetResource(ResourceType ID);
StatusType      ReleaseResource(ResourceType ID);

#ifdef   __cplusplus
        }
#endif

#endif

/***************************************************************************************/
/*                         End of File : uip_kernel_api.h                              */
/***************************************************************************************/
