⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_cpu_c.c

📁 windows vc ucos
💻 C
📖 第 1 页 / 共 2 页
字号:
            interruptTable[4]();
        } else if (eventType==WAIT_OBJECT_0+5)
        {   ResetEvent(hInterruptEvent[5]);
            interruptTable[5]();
        } else if (eventType==WAIT_OBJECT_0+6)
        {   ResetEvent(hInterruptEvent[6]);
            interruptTable[6]();
        } else if (eventType==WAIT_OBJECT_0+7)
        {   ResetEvent(hInterruptEvent[7]);
            interruptTable[7]();
        } else
        {   MessageBox(NULL, temp, "UCOS-II WIN32", MB_OK | MB_SETFOREGROUND | MB_ICONERROR);		//In case of timeout, display an error message ...
        }
        DBGPRINT(0x00000001, "--- OSIntExit\n");
        OSIntExit();
        LeaveCriticalSection(&criticalSection);
    }
}


/*
*********************************************************************************************************
   uCOS-II Functions
*********************************************************************************************************
*/

// OSTimeTickInit ************************************************************
// Initialize the WIN32 multimedia timer to simulate time tick interrupts
void OSTimeTickInit()
{   TIMECAPS timecaps;

    DBGPRINT(0x00000008, "*** OSTimeTickInit\n");

    if (timeGetDevCaps((LPTIMECAPS) & timecaps, sizeof(timecaps)) != TIMERR_NOERROR)
    {   printf("uCOS-II ERROR: Timer could not be installed 1\n");
        exit(-1);
    }
    if (timeBeginPeriod(timecaps.wPeriodMin) != TIMERR_NOERROR)
    {   printf("uCOS-II ERROR: Timer could not be installed 2\n");
        exit(-1);
    }
    if (timeSetEvent(1000 / OS_TICKS_PER_SEC, 0, (LPTIMECALLBACK) OSTimeTickCallback, 0, TIME_PERIODIC) == 0)
    {   printf("uCOS-II ERROR: Timer could not be installed 3\n");
        exit(-1);
    }
}

// OSTaskStkInit *************************************************************
// This function does initialize the stack of a task (this is only a dummy function
// for compatibility reasons, because this stack is not really used (except to refer
// to the task parameter *pdata)
OS_STK *OSTaskStkInit(void (*task) (void *pd), void *pdata, OS_STK *ptos, INT16U opt)
{   OS_STK *stk;

    DBGPRINT(0x00000010, "*** OSTaskStkInit\n");

    stk = (OS_STK *) ptos;                                      // Load stack pointer
    *--stk = (INT32U) pdata;					// Push pdata on the stack
    *--stk = (INT32U) task;					// Push the task start address on the stack

    return stk;
}


/*
*******************************************************************************
   Internal Hook functions (used by the WIN32 port, not user hookable)
*******************************************************************************
*/

// OSInitHookBegin ***********************************************************
// This hook is invoked at the beginning of the OS initialization. MUST NOT BE DEFINED BY THE APPLICATION.
void OSInitHookBegin()
{   int i;
    char temp[256];
    DBGPRINT(0x00000010, "*** OSInitHookBegin\n");

    hScheduleEvent  = CreateEvent(NULL, FALSE, FALSE, NULL);	//Create the scheduler and interrupt event

    for (i=0; i< NINTERRUPTS; i++)
    {	sprintf(temp,"OSirq%u",i);
    	hInterruptEvent[i] = CreateEvent(NULL, TRUE, FALSE, temp);
        interruptTable[i]  = OSDummyISR;
    }

    InitializeCriticalSection(&criticalSection);
    hScheduleThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) OSScheduleThread,  NULL, 0, &i);	//Create the scheduler and interrupt thread
    hInterruptThread= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) OSInterruptThread, NULL, 0, &interruptThreadId);
    interruptTable[0] = OSTimeTick;

//  SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
    SetThreadPriority(hScheduleThread,  THREAD_PRIORITY_HIGHEST);//Set the scheduler and interrupt threads to maximum WIN32 priority
    SetThreadPriority(hInterruptThread, THREAD_PRIORITY_HIGHEST);

    SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlBreakHandler, TRUE);
    Sleep(0);							//Give Windows a chance to start the scheduler and interrupt thread

    //ADD YOUR OWN HOOK CODE HERE IF NECESSARY
}

// OSTaskCreateHook **********************************************************
// This hook is invoked during task creation. MUST NOT BE DEFINED BY THE APPLICATION.
void OSTaskCreateHook(OS_TCB * pTcb)
{   int i;

    DBGPRINT(0x00000004, "*** OSTaskCreateHook %u\n", pTcb->OSTCBPrio);

    hTaskThread[pTcb->OSTCBPrio] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) *(pTcb->OSTCBStkPtr),	//Map uCOS-II task to WIN32 thread
                                                (void *) *(pTcb->OSTCBStkPtr + 1), CREATE_SUSPENDED, &i);
    taskSuspended[pTcb->OSTCBPrio]=1;									//Create thread in WIN32 suspended state

    if (OS_BOOST_WIN32_PRIORITY && (pTcb->OSTCBPrio!=OS_LOWEST_PRIO))           //Boost WIN32 thread priorites (except idle thread)
        SetThreadPriority(hTaskThread[pTcb->OSTCBPrio], THREAD_PRIORITY_ABOVE_NORMAL);

    //ADD YOUR OWN HOOK CODE HERE IF NECESSARY
}

// OSTaskIdleHook ************************************************************
// This hook is invoked from the idle task. MUST NOT BE DEFINED BY THE APPLICATION.
void OSTaskIdleHook()
{   if (idleTrigger)						//Issue a debug message, each time the idle task is reinvoked
    {   DBGPRINT(0x00000020, "*** OSTaskIdleHook\n");
        idleTrigger = FALSE;
    }

    //ADD YOUR OWN HOOK CODE HERE IF NECESSARY

    Sleep(0);							//Give Windows a chance to run other applications to when uCOS-II idles
}

/*
*******************************************************************************
   Hook functions (user hookable)
*******************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0

/*
void OSInitHookBegin()                  MUST NOT BE DEFINED BY THE APPLICATION, see above.
*/

// OSInitHookEnd *************************************************************
// This hook is invoked at the end of the OS initialization.
void OSInitHookEnd()
{
}

/*
void OSTaskCreateHook(OS_TCB * pTcb)    MUST NOT BE DEFINED BY THE APPLICATION, see above.
*/

// OSTaskDelHook *************************************************************
// This hook is invoked during task deletion.
void OSTaskDelHook(OS_TCB * pTcb)
{
}

// OSTCBInitHook
// This hook is invoked during TCB initialization
void OSTCBInitHook(OS_TCB * ptcb)
{
}

/*
void OSTaskIdleHook()                   MUST NOT BE DEFINED BY THE APPLICATION, see above.
*/

// OSTaskStatHook ************************************************************
// This hook is invoked by the statistical task every second.
void OSTaskStatHook()
{
}

// OSTimeTickHook ************************************************************
// This hook is invoked during a time tick.
void OSTimeTickHook()
{
}

// OSTaskSwHook **************************************************************
// This hook is invoked during a task switch.
// OSTCBCur points to the current task (being switched out).
// OSTCBHighRdy points on the new task (being switched in).
void OSTaskSwHook()
{
}

#else
#pragma message("INFO: Hook functions must be defined in the application")
#endif

/*
*******************************************************************************
   Internal Task switch functions
*******************************************************************************
*/

// OSStartHighRdy *************************************************************
// Start first task
void OSStartHighRdy(void)
{   OSTaskSwHook();						//Call the task switch hook function

    OSTimeTickInit();                                           //Initialize time ticks

    // Increment OSRunning by 1
    OSRunning++;

    DBGPRINT(0x00000002, "*** OSStartHighRdy   from %2u to %2u\n", OSPrioCur, OSPrioHighRdy);

    SetEvent(hScheduleEvent);                                   //Trigger scheduling thread

    while(OSRunning)						//A redundant idle thread, in case the uCOS
    {   Sleep(0);						//scheduler does not invoke the uCOS idle task
    }
    printf("ERROR: Primary thread killed - exiting\n");
}

// OSCtxSw ********************************************************************
// Task context switch
void OSCtxSw(void)
{   OSTaskSwHook();						//Call the task switch hook function

    DBGPRINT(0x00000002, "*** OSCtxSw          from %2u to %2u\n", OSPrioCur, OSPrioHighRdy);

    SetEvent(hScheduleEvent);                                   //Trigger scheduling thread
    Sleep(0);							//... and give Windows a chance to invoke it
}

// OSIntCtxSw *****************************************************************
// Interrupt context switch
void OSIntCtxSw(void)
{   OSTaskSwHook();						//Call the task switch hook function

    DBGPRINT(0x00000002, "*** OSCIntCtxSw      from %2u to %2u\n", OSPrioCur, OSPrioHighRdy);

    SetEvent(hScheduleEvent);                                   //Trigger scheduling thread
    Sleep(0);							//... and give Windows a chance to invoke it
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -