vs2008_ucos\uCOSII_port\os_cfg.h os_cpu.h os_cpu_c.c[part1]--移植ucosii在VS2008--part2

来源:互联网 发布:smtp.qq. 端口号 编辑:程序博客网 时间:2024/04/29 23:12

vs2008_ucos\uCOSII_port\os_cfg.h os_cpu.h os_cpu_c.c[part1]

/*****************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*                                  uC/OS-II Configuration File for V2.8x
*
*                               (c) Copyright 2005-2007, Micrium, Weston, FL
*                                           All Rights Reserved
* File    : OS_CFG.H
* By      : Jean J. Labrosse
* Version : V2.86
******************************************************************************************/
#ifndef OS_CFG_H
#define OS_CFG_H
                                       /* ---------------------- MISCELLANEOUS ----------------------- */
#define OS_APP_HOOKS_EN           0    /* Application-defined hooks are called from the uC/OS-II hooks */
#define OS_ARG_CHK_EN             0    /* Enable (1) or Disable (0) argument checking                  */
#define OS_CPU_HOOKS_EN           1    /* uC/OS-II hooks are found in the processor port files         */

#define OS_DEBUG_EN               1    /* Enable(1) debug variables                                    */

#define OS_EVENT_MULTI_EN         1    /* Include code for OSEventPendMulti()                          */
#define OS_EVENT_NAME_SIZE       32    /* Determine the size of the name of a Sem, Mutex, Mbox or Q    */

#define OS_LOWEST_PRIO           63    /* Defines the lowest priority that can be assigned ...         */
                                       /* ... MUST NEVER be higher than 254!                           */

#define OS_MAX_EVENTS           100    /* Max. number of event control blocks in your application      */
#define OS_MAX_FLAGS              5    /* Max. number of Event Flag Groups    in your application      */
#define OS_MAX_MEM_PART           5    /* Max. number of memory partitions                             */
#define OS_MAX_QS                 4    /* Max. number of queue control blocks in your application      */
#define OS_MAX_TASKS             20    /* Max. number of tasks in your application, MUST be >= 2       */

#define OS_SCHED_LOCK_EN          1    /*     Include code for OSSchedLock() and OSSchedUnlock()       */

#define OS_TICK_STEP_EN           1    /* Enable tick stepping feature for uC/OS-View                  */
#define OS_TICKS_PER_SEC        200    /* Set the number of ticks in one second                        */

                                       /* --------------------- TASK STACK SIZE ---------------------- */
#define OS_TASK_TMR_STK_SIZE    128    /* Timer      task stack size (# of OS_STK wide entries)        */
#define OS_TASK_STAT_STK_SIZE   128    /* Statistics task stack size (# of OS_STK wide entries)        */
#define OS_TASK_IDLE_STK_SIZE   128    /* Idle       task stack size (# of OS_STK wide entries)        */

                                       /* --------------------- TASK MANAGEMENT ---------------------- */
#define OS_TASK_CHANGE_PRIO_EN    0    /*     Include code for OSTaskChangePrio()                      */
#define OS_TASK_CREATE_EN         1    /*     Include code for OSTaskCreate()                          */
#define OS_TASK_CREATE_EXT_EN     1    /*     Include code for OSTaskCreateExt()                       */
#define OS_TASK_DEL_EN            1    /*     Include code for OSTaskDel()                             */
#define OS_TASK_NAME_SIZE        32    /*     Determine the size of a task name                        */
#define OS_TASK_PROFILE_EN        1    /*     Include variables in OS_TCB for profiling                */
#define OS_TASK_QUERY_EN          1    /*     Include code for OSTaskQuery()                           */
#define OS_TASK_STAT_EN           1    /* Enable (1) or Disable(0) the statistics task                 */
#define OS_TASK_STAT_STK_CHK_EN   1    /* Check task stacks from statistic task                        */
#define OS_TASK_SUSPEND_EN        1    /*     Include code for OSTaskSuspend() and OSTaskResume()      */
#define OS_TASK_SW_HOOK_EN        1    /*     Include code for OSTaskSwHook()                          */

                                       /* ----------------------- EVENT FLAGS ------------------------ */
#define OS_FLAG_EN                1    /* Enable (1) or Disable (0) code generation for EVENT FLAGS    */
#define OS_FLAG_ACCEPT_EN         1    /*     Include code for OSFlagAccept()                          */
#define OS_FLAG_DEL_EN            1    /*     Include code for OSFlagDel()                             */
#define OS_FLAG_NAME_SIZE        32    /*     Determine the size of the name of an event flag group    */
#define OS_FLAG_QUERY_EN          1    /*     Include code for OSFlagQuery()                           */
#define OS_FLAG_WAIT_CLR_EN       1    /*     Include code for Wait on Clear EVENT FLAGS               */
#define OS_FLAGS_NBITS           16    /* Size in #bits of OS_FLAGS data type (8, 16 or 32)            */

                                       /* -------------------- MESSAGE MAILBOXES --------------------- */
#define OS_MBOX_EN                1    /* Enable (1) or Disable (0) code generation for MAILBOXES      */
#define OS_MBOX_ACCEPT_EN         1    /*     Include code for OSMboxAccept()                          */
#define OS_MBOX_DEL_EN            1    /*     Include code for OSMboxDel()                             */
#define OS_MBOX_PEND_ABORT_EN     1    /*     Include code for OSMboxPendAbort()                       */
#define OS_MBOX_POST_EN           1    /*     Include code for OSMboxPost()                            */
#define OS_MBOX_POST_OPT_EN       1    /*     Include code for OSMboxPostOpt()                         */
#define OS_MBOX_QUERY_EN          1    /*     Include code for OSMboxQuery()                           */

                                       /* --------------------- MEMORY MANAGEMENT -------------------- */
#define OS_MEM_EN                 0    /* Enable (1) or Disable (0) code generation for MEMORY MANAGER */
#define OS_MEM_NAME_SIZE         32    /*     Determine the size of a memory partition name            */
#define OS_MEM_QUERY_EN           0    /*     Include code for OSMemQuery()                            */

                                       /* ---------------- MUTUAL EXCLUSION SEMAPHORES --------------- */
#define OS_MUTEX_EN               1    /* Enable (1) or Disable (0) code generation for MUTEX          */
#define OS_MUTEX_ACCEPT_EN        1    /*     Include code for OSMutexAccept()                         */
#define OS_MUTEX_DEL_EN           1    /*     Include code for OSMutexDel()                            */
#define OS_MUTEX_QUERY_EN         1    /*     Include code for OSMutexQuery()                          */

                                       /* ---------------------- MESSAGE QUEUES ---------------------- */
#define OS_Q_EN                   1    /* Enable (1) or Disable (0) code generation for QUEUES         */
#define OS_Q_ACCEPT_EN            1    /*     Include code for OSQAccept()                             */
#define OS_Q_DEL_EN               1    /*     Include code for OSQDel()                                */
#define OS_Q_FLUSH_EN             1    /*     Include code for OSQFlush()                              */
#define OS_Q_PEND_ABORT_EN        1    /*     Include code for OSQPendAbort()                          */
#define OS_Q_POST_EN              1    /*     Include code for OSQPost()                               */
#define OS_Q_POST_FRONT_EN        1    /*     Include code for OSQPostFront()                          */
#define OS_Q_POST_OPT_EN          1    /*     Include code for OSQPostOpt()                            */
#define OS_Q_QUERY_EN             0    /*     Include code for OSQQuery()                              */

                                       /* ------------------------ SEMAPHORES ------------------------ */
#define OS_SEM_EN                 1    /* Enable (1) or Disable (0) code generation for SEMAPHORES     */
#define OS_SEM_ACCEPT_EN          1    /*    Include code for OSSemAccept()                            */
#define OS_SEM_DEL_EN             1    /*    Include code for OSSemDel()                               */
#define OS_SEM_PEND_ABORT_EN      1    /*    Include code for OSSemPendAbort()                         */
#define OS_SEM_QUERY_EN           1    /*    Include code for OSSemQuery()                             */
#define OS_SEM_SET_EN             1    /*    Include code for OSSemSet()                               */

                                       /* --------------------- TIME MANAGEMENT ---------------------- */
#define OS_TIME_DLY_HMSM_EN       1    /*     Include code for OSTimeDlyHMSM()                         */
#define OS_TIME_DLY_RESUME_EN     0    /*     Include code for OSTimeDlyResume()                       */
#define OS_TIME_GET_SET_EN        1    /*     Include code for OSTimeGet() and OSTimeSet()             */
#define OS_TIME_TICK_HOOK_EN      1    /*     Include code for OSTimeTickHook()                        */

                                       /* --------------------- TIMER MANAGEMENT --------------------- */
#define OS_TMR_EN                 0    /* Enable (1) or Disable (0) code generation for TIMERS         */
#define OS_TMR_CFG_MAX           16    /*     Maximum number of timers                                 */
#define OS_TMR_CFG_NAME_SIZE     16    /*     Determine the size of a timer name                       */
#define OS_TMR_CFG_WHEEL_SIZE     8    /*     Size of timer wheel (#Spokes)                            */
#define OS_TMR_CFG_TICKS_PER_SEC 10    /*     Rate at which timer management task runs (Hz)            */
#endif

/*****************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*                                     Microsoft Win32 Specific code
*                                 (c) Copyright 2008; Micrium; Weston, FL
*                                           All Rights Reserved
* File    : os_cpu.h
* By      : FGK
* LICENSING TERMS:
* ---------------
*****************************************************************************************/
#include  <cpu.h>

#ifdef    OS_CPU_GLOBALS
    #define   OS_CPU_EXT
    #else
        #define   OS_CPU_EXT  extern
    #endif
/*****************************************************************************************
*                                           DATA TYPES
*****************************************************************************************/

typedef  CPU_BOOLEAN    BOOLEAN;
typedef  CPU_INT08U     INT8U;              /* Unsigned  8 bit quantity             */
typedef  CPU_INT08S     INT8S;              /* Signed    8 bit quantity             */
typedef  CPU_INT16U     INT16U;             /* Unsigned 16 bit quantity             */
typedef  CPU_INT16S     INT16S;             /* Signed   16 bit quantity             */
typedef  CPU_INT32U     INT32U;             /* Unsigned 32 bit quantity             */
typedef  CPU_INT32S     INT32S;             /* Signed   32 bit quantity             */
typedef  CPU_FP32       FP32;               /* Single precision floating point      */
typedef  CPU_FP64       FP64;               /* Double precision floating point      */
typedef  CPU_STK        OS_STK;             /* Define size of CPU stack entry       */
typedef  CPU_SR         OS_CPU_SR;          /* Define size of CPU status register   */

/*****************************************************************************************
*                                           Critical Method MACROS
*****************************************************************************************/
#define  OS_CRITICAL_METHOD     1
#define  OS_ENTER_CRITICAL()  { OSCriticalEnter(); }
#define  OS_EXIT_CRITICAL()   { OSCriticalExit();  }

/*****************************************************************************************
*                                          Miscellaneous
*****************************************************************************************/
#define  OS_STK_GROWTH      1            /* Stack grows from HIGH to LOW memory on WIN32 */
#define  OS_TASK_SW()    { OSCtxSw(); }  /* Perform task switch                          */

/*****************************************************************************************
*                                         Function Prototypes
*****************************************************************************************/
void  OSCtxSw(void);
void  OSIntCtxSw(void);
void  OSStartHighRdy(void);
void  OSCriticalEnter(void);
void  OSCriticalExit(void);

/***************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*                                     Microsoft Win32 Specific code
*                                 (c) Copyright 2008; Micrium; Weston, FL
*                                           All Rights Reserved
* File    : os_cpu_c.c
* By      : FGK
* LICENSING TERMS:
* ---------------
*   uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
* If you plan on using  uC/OS-II  in a commercial product you need to contact Micri祄 to properly  license
* its use in your product.  We provide ALL the source code for your convenience and to help you experience
* uC/OS-II.   The fact that the  source  is provided does  NOT  mean that you can use it without  paying a
* licensing fee.
***************************************************************************************************/
#define   OS_CPU_GLOBALS
/***************************************************************************************************
*                                             INCLUDE FILES
***************************************************************************************************/
#define  _WIN32_WINNT  0x0600
#include  <ucos_ii.h>
#include  <windows.h>
#include  <mmsystem.h>
#include  <stdio.h>
/***************************************************************************************************
*                                             LOCAL DEFINES
***************************************************************************************************/
#define  WIN32_CRITICAL_SECTION                   1
#define  WIN32_MUTEX                              2      /* Allow recursion of critical sections. */
#define  CRITICAL_METHOD          WIN32_CRITICAL_SECTION
#define  WIN32_SLEEP                              1
#define  WIN32_MM_TMR                             2      /* Use the high resolution Multimedia timer.*/
#define  TIMER_METHOD             WIN32_MM_TMR
#define  WIN_MM_MIN_RES                          (1)     /* Minimum timer resolution.             */
#define  OS_MSG_TRACE                            (1)     /* Allow print trace messages.           */
#define  MS_VC_EXCEPTION                 0x406D1388
/***************************************************************************************************
*                                            LOCAL DATA TYPES
***************************************************************************************************/
typedef  enum  os_task_state {
    STATE_NONE = 0,
    STATE_CREATED,
    STATE_RUNNING,
    STATE_SUSPENDED,
    STATE_INTERRUPTED,
    STATE_TERMINATING,
    STATE_TERMINATED
} OS_TASK_STATE;

typedef  struct  os_task_stk {
    void            *TaskArgPtr;
    INT16U           TaskOpt;
    void           (*Task)(void*);
    HANDLE           ThreadHandle;
    INT32U           ThreadID;
    OS_TASK_STATE    TaskState;
    HANDLE           SignalPtr;
} OS_TASK_STK;

#pragma pack(push,8)
typedef  struct  threadname_info {
    DWORD   dwType;             /* Must be 0x1000.                                          */
    LPCSTR  szName;             /* Pointer to name (in user addr space).                    */
    DWORD   dwThreadID;         /* Thread ID (-1 = caller thread).                          */
    DWORD   dwFlags;            /* Reserved for future use, must be zero.                   */
} THREADNAME_INFO;
#pragma pack(pop)


#if (TIMER_METHOD == WIN32_MM_TMR)
#pragma  comment (lib, "winmm.lib")
#endif

/***************************************************************************************************
*                                            LOCAL VARIABLES
***************************************************************************************************/

#if (OS_VERSION >= 281) && (OS_TMR_EN > 0)
static  INT16U              OSTmrCtr;
#endif                          /* #if (OS_VERSION >= 281) && (OS_TMR_EN > 0)               */
static  HANDLE              OSTerminate_SignalPtr;
static  HANDLE              OSTick_Thread;
static  DWORD               OSTick_ThreadId;
#if (TIMER_METHOD == WIN32_MM_TMR)
static  HANDLE              OSTick_SignalPtr;
static  TIMECAPS            OSTick_TimerCap;
static  MMRESULT            OSTick_TimerId;
#endif
#if   (CRITICAL_METHOD == WIN32_CRITICAL_SECTION)
static  CRITICAL_SECTION    OSCriticalSection;
#elif (CRITICAL_METHOD == WIN32_MUTEX)
static  HANDLE              OSMutex;
#endif

/***************************************************************************************************
*                                       LOCAL FUNCTION PROTOTYPES
***************************************************************************************************/

static  DWORD  WINAPI   OSCtxSwW32          (LPVOID  lpParameter);
static  DWORD  WINAPI   OSTickW32           (LPVOID  lpParameter);
static  DWORD  WINAPI   OSTaskW32           (LPVOID  lpParameter);

static  BOOLEAN         OSCtrlBreakHandler  (INT32U  ctrl);

static  void            OSDebuggerBreak     (void);

static  void            OSSetThreadName     (DWORD   dwThreadID,
                                             INT8U  *strThreadName);

#if (OS_MSG_TRACE > 0)
static  int             OSPrintf            (char   *str, ...);
#endif

/***************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                            (BEGINNING)
* Description: This function is called by OSInit() at the beginning of OSInit().
* Arguments  : none
* Note(s)    : 1) Interrupts should be disabled during this call.
*
*              2) Kernel objects must have unique names. Otherwise, a duplicate handle will be given for
*                 consecutive created objects. A GetLastError() ERROR_ALREADY_EXISTS can be checked when
*                 this case happens.
***************************************************************************************************/
#if (OS_CPU_HOOKS_EN > 0) && (OS_VERSION > 203)
void  OSInitHookBegin (void)
{
    HANDLE   hProc;
#if (CRITICAL_METHOD == WIN32_MUTEX) && \
    (OS_MSG_TRACE > 0)
    DWORD    last_err;
    LPTSTR   pmsg;
    OS_TCB  *ptcb;
#endif


#if (OS_VERSION >= 281) && (OS_TMR_EN > 0)
    OSTmrCtr = 0;
#endif

#if (TIMER_METHOD == WIN32_SLEEP) && \
    (OS_TICKS_PER_SEC > 100) && \
    (OS_MSG_TRACE == TRUE)
    OSPrintf("Warning: Sleep TIMER_METHOD cannot maintain time accuracy with the current setting of OS_TICKS_PER_SEC. Consider using Multimedia TIMER_METHOD.\n\n");
#endif

#if   (CRITICAL_METHOD == WIN32_CRITICAL_SECTION)
    InitializeCriticalSection(&OSCriticalSection);
#elif (CRITICAL_METHOD == WIN32_MUTEX)
    OSMutex = CreateMutex(NULL, FALSE, NULL);
    if (OSMutex == NULL) {
#if (OS_MSG_TRACE > 0)
        last_err = GetLastError();

        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                      NULL,
                      last_err,
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                      (LPTSTR)&pmsg,
                      0,
                      NULL);

        ptcb = OSTCBCur;
        OSPrintf("Error: Initialize Critical Section failed: %s.\n", pmsg);

        LocalFree(pmsg);
#endif
        return;
    }
#endif
    hProc = GetCurrentProcess();
    SetPriorityClass(hProc, HIGH_PRIORITY_CLASS);
    OSSetThreadName(GetCurrentThreadId(), "main()");
    OSTerminate_SignalPtr = CreateEvent(NULL, TRUE, FALSE, NULL);       /* Manual reset enabled to broadcast terminate signal.      */
    if (OSTerminate_SignalPtr == NULL) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: CreateEvent [OSTerminate] failed.\n");
#endif
        return;
    }
    SetConsoleCtrlHandler((PHANDLER_ROUTINE)OSCtrlBreakHandler, TRUE);

    OSTick_Thread = CreateThread(NULL, 0, OSTickW32, 0, CREATE_SUSPENDED, &OSTick_ThreadId);
    if (OSTick_Thread == NULL) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: CreateThread [OSTickW32] failed.\n");
#endif
        CloseHandle(OSTerminate_SignalPtr);
        return;
    }

#if (OS_MSG_TRACE > 0)
    OSPrintf("OSTick    created, Thread ID %5.0d\n", OSTick_ThreadId);
#endif

    SetThreadPriority(OSTick_Thread, THREAD_PRIORITY_HIGHEST);
    if (SetThreadAffinityMask(OSTick_Thread, 1) == 0) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: SetThreadAffinityMask [OSTickW32] failed.\n");
#endif
    }

#if (TIMER_METHOD == WIN32_MM_TMR)
    if (timeGetDevCaps(&OSTick_TimerCap, sizeof(OSTick_TimerCap)) != TIMERR_NOERROR) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: Cannot retrieve Timer capabilities.\n");
#endif
        CloseHandle(OSTick_Thread);
        CloseHandle(OSTerminate_SignalPtr);
        return;
    }

    if (OSTick_TimerCap.wPeriodMin < WIN_MM_MIN_RES) {
        OSTick_TimerCap.wPeriodMin = WIN_MM_MIN_RES;
    }

    if (timeBeginPeriod(OSTick_TimerCap.wPeriodMin) != TIMERR_NOERROR) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: Cannot set Timer minimum resolution.\n");
#endif
        CloseHandle(OSTick_Thread);
        CloseHandle(OSTerminate_SignalPtr);
        return;
    }
    OSTick_SignalPtr = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (OSTick_SignalPtr == NULL) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: CreateEvent [OSTick] failed.\n");
#endif
        timeEndPeriod(OSTick_TimerCap.wPeriodMin);
        CloseHandle(OSTick_Thread);
        CloseHandle(OSTerminate_SignalPtr);
        return;
    }

#pragma warning (disable : 4055)
    OSTick_TimerId = timeSetEvent((UINT          )(1000 / OS_TICKS_PER_SEC),
                                  (UINT          ) OSTick_TimerCap.wPeriodMin,
                                  (LPTIMECALLBACK) OSTick_SignalPtr,
                                  (DWORD_PTR     ) NULL,
                                  (UINT          )(TIME_PERIODIC | TIME_CALLBACK_EVENT_SET));
#pragma warning (default : 4055)
    if (OSTick_TimerId == 0) {
#if (OS_MSG_TRACE > 0)
        OSPrintf("Error: Cannot start Timer.\n");
#endif
        CloseHandle(OSTick_SignalPtr);
        timeEndPeriod(OSTick_TimerCap.wPeriodMin);
        CloseHandle(OSTick_Thread);
        CloseHandle(OSTerminate_SignalPtr);
        return;
    }
#endif
}
#endif

/***************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                               (END)
* Description: This function is called by OSInit() at the end of OSInit().
* Arguments  : none
* Note(s)    : 1) Interrupts should be disabled during this call.
***************************************************************************************************/
#if (OS_CPU_HOOKS_EN > 0) && (OS_VERSION > 203)
void  OSInitHookEnd (void)
{
}
#endif
/***************************************************************************************************
*                                          TASK CREATION HOOK
* Description: This function is called when a task is created.
* Arguments  : ptcb   is a pointer to the task control block of the task being created.
* Note(s)    : 1) Interrupts are disabled during this call.
***************************************************************************************************/
#if (OS_CPU_HOOKS_EN > 0)
void  OSTaskCreateHook (OS_TCB  *ptcb)
{
#if (OS_APP_HOOKS_EN > 0)
    App_TaskCreateHook(ptcb);
#else
    (void)ptcb;                /* Prevent compiler warning                                 */
#endif
}
#endif
/***************************************************************************************************
*                                           TASK DELETION HOOK
*
* Description: This function is called when a task is deleted.
*
* Arguments  : ptcb   is a pointer to the task control block of the task being deleted.
*
* Note(s)    : 1) Interrupts are disabled during this call.
***************************************************************************************************/
#if (OS_CPU_HOOKS_EN > 0)
void  OSTaskDelHook (OS_TCB  *ptcb)
{
    OS_TASK_STK  *pstack;
#if (OS_APP_HOOKS_EN > 0)
    App_TaskDelHook(ptcb);
#else
    (void)ptcb;                         /* Prevent compiler warning                          */
#endif

    pstack = (OS_TASK_STK *)ptcb->OSTCBStkPtr;

    switch (pstack->TaskState) {
        case STATE_CREATED:
        case STATE_RUNNING:
        case STATE_SUSPENDED:
        case STATE_INTERRUPTED:
             TerminateThread(pstack->ThreadHandle, -1);
             CloseHandle(pstack->ThreadHandle);
                                         /* Intentional fall through to STATE_TERMINATING.    */
        case STATE_TERMINATING:
             CloseHandle(pstack->SignalPtr);
             pstack->TaskState = STATE_TERMINATED;
             break;

        default:
             break;
    }
}
#endif

 

原创粉丝点击