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

📄 emos_cpu.c

📁 emos是一个新的类似于ucos的内核
💻 C
📖 第 1 页 / 共 2 页
字号:
* Description: This function is called by the idle task.
* Arguments  : none
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
**********************************************************************************************************/
void emosTaskIdleHook (void)
{
}
#endif

void emosStartHighRdy()
{
    DWORD  dwID;

    /*emosInitTrace(100000);*/

    EMOS_ENTER_CRITICAL();

    emosTaskSwHook();
    gEmosRunning = EMOS_TRUE;

    /*This thread is used for Ctx Switch to simulation Hardware Ctx Switch*/
    gEmosCtxSwW32Event  = CreateEvent(NULL,FALSE,FALSE,NULL);
    gEmosCtxSwW32Handle = CreateThread( NULL, 0, emosCtxSwW32, 0, 0, &dwID );

    /*Set the Ctx thread to highest priority*/
    SetPriorityClass(gEmosCtxSwW32Handle,THREAD_PRIORITY_HIGHEST);

    #ifdef SET_AFFINITY_MASK
    if( SetThreadAffinityMask(gEmosCtxSwW32Handle, 1 ) == 0 ) 
    {
        #ifdef EMOS_CPU_TRACE
        EMOS_Printf("Error SetThreadAffinityMask\n");
        #endif
    }
    #endif
    
	SetThreadPriority(gEmosCtxSwW32Handle,THREAD_PRIORITY_TIME_CRITICAL);

    /*This thread is used for Tick Simulation*/
    gEmosTick32Handle = CreateThread( NULL, 0, emosTickW32, 0, 0, &dwID );
    SetPriorityClass(gEmosTick32Handle,THREAD_PRIORITY_HIGHEST);

    #ifdef SET_AFFINITY_MASK
    if( SetThreadAffinityMask(gEmosTick32Handle, 1 ) == 0 ) 
	{
        #ifdef EMOS_CPU_TRACE
        EMOS_Printf("Error: SetThreadAffinityMask\r\n");
        #endif
    }
    #endif

	SetThreadPriority(gEmosTick32Handle,THREAD_PRIORITY_HIGHEST);

   #ifdef WIN_MM_TICK
    timeGetDevCaps(&gEmosTimeCap, sizeof(gEmosTimeCap));

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

    timeBeginPeriod(gEmosTimeCap.wPeriodMin);

    gEmosTickEventHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
    gEmosTickTimer       = timeSetEvent((1000/EMOS_TICKS_PER_SEC),gEmosTimeCap.wPeriodMin,(LPTIMECALLBACK)gEmosTickEventHandle, dwID,TIME_PERIODIC|TIME_CALLBACK_EVENT_SET);
    #endif
    
    /*Begin the highest priority task-thread */ 
    SS_SP = (EMOS_EMU_STK*) gEmosTCBHighRdy->osTCBStkPtr; 
    emosTaskSwHook();
    
    ResumeThread(SS_SP->handle);

    EMOS_EXIT_CRITICAL();

    #if 1
    /*Here waiting for the event?*/
    WaitForSingleObject(gEmosCtxSwW32Handle,INFINITE);
    
    #ifdef WIN_MM_TICK
    timeKillEvent(gEmosTickTimer);
    timeEndPeriod(gEmosTimeCap.wPeriodMin);
    CloseHandle(gEmosTickEventHandle);
    #endif

	CloseHandle(gEmosTick32Handle);
    CloseHandle(gEmosCtxSwW32Event);
    #endif
}

void emosCtxSw()
{
    DWORD n = 0;

    if(!(SS_SP->exit)) 
    {
        n = SuspendThread(SS_SP->handle);
    }
    
    emosTaskSwHook();

    /*OSTrace( OBJ_SW, PT_SW_CTX, gEmosTCBHighRdy, 0, gEmosPrioCur, gEmosPrioHighRdy,0 );*/
    gEmosTCBCur = gEmosTCBHighRdy;
    gEmosPrioCur = gEmosPrioHighRdy;
    SS_SP = (EMOS_EMU_STK*) gEmosTCBHighRdy->osTCBStkPtr;
    ResumeThread(SS_SP->handle);
}

void emosIntCtxSw()
{
    DWORD n = 0;

    if(!(SS_SP->exit)) 
    {
        n = SuspendThread(SS_SP->handle);
    }

    emosTaskSwHook();

    /*OSTrace( OBJ_SW, PT_SW_INT, OSTCBHighRdy, 0, OSPrioCur,OSPrioHighRdy,0 );*/
    gEmosTCBCur = gEmosTCBHighRdy;
    gEmosPrioCur = gEmosPrioHighRdy;
    SS_SP = (EMOS_EMU_STK*) gEmosTCBHighRdy->osTCBStkPtr;
    ResumeThread(SS_SP->handle);
}

void emosTickISR()
{
    emosIntEnter();
    emosTimeTick();
    emosIntExit();
}

/*********************************************************************************************************
* WIN32 TASK - OSCtxSwW32()
* Description: These functions are body of OS multitasking in WIN32.
* Arguments  : lpParameter   is a pointer to special paraleter of the task.
* Note(s)    : 1) Priorities of these tasks are very important.
**********************************************************************************************************/
DWORD WINAPI emosCtxSwW32( LPVOID lpParameter )
{
    EMOS_INIT_CRITICAL();

    while(!gEmosTerminateCtxSwW32)
    {
        if( WAIT_OBJECT_0 == WaitForSingleObject(gEmosCtxSwW32Event,INFINITE) )
        {
            EMOS_ENTER_CRITICAL();
            emosCtxSw();
            EMOS_EXIT_CRITICAL();
        }
    }
    
    return 0;
}

/*********************************************************************************************************
* WIN32 TASK - OSTickW32()
* Description: These functions are body of OS multitasking in WIN32.
* Arguments  : lpParameter   is a pointer to special paraleter of the task.
* Note(s)    : 1) Priorities of these tasks are very important.
**********************************************************************************************************/
DWORD WINAPI emosTickW32( LPVOID lpParameter )
{
    EMOS_INIT_CRITICAL();

    while(!gEmosTerminateTickW32)
    {
        emosTickISR();
        #ifdef WIN_MM_TICK
        if(WaitForSingleObject(gEmosTickEventHandle, 5000) == WAIT_TIMEOUT)
        {
            #ifdef EMOS_CPU_TRACE
            EMOS_Printf("Error MM OSTick Timeout!\n");
            #endif
        }

        ResetEvent(gEmosTickEventHandle);
        #else
        Sleep(1000/EMOS_TICKS_PER_SEC);
        #endif
    }

    return 0;
}

/********************************************************************************************************
* WIN32 TASK - OSTaskW32()
* Description: These functions are body of OS multitasking in WIN32.
* Arguments  : lpParameter   is a pointer to special paraleter of the task.
* Note(s)    : 1) Priorities of these tasks are very important.
**********************************************************************************************************/
DWORD WINAPI emosTaskW32( LPVOID lpParameter )
{
    EMOS_TCB_T*    ptcb;
    EMOS_EMU_STK*  stack;

    ptcb = (EMOS_TCB_T*) lpParameter;
    stack = (EMOS_EMU_STK*) ptcb->osTCBStkPtr;
    
     #ifdef DISABLE_PRIORITY_BOOST
     if( SetThreadPriorityBoost( stack->handle, TRUE ) == 0 )
     {
            #ifdef EMOS_CPU_TRACE
            EMOS_Printf("Error: SetThreadPriorityBoost\n");
            #endif
     }
     #endif

    EMOS_INIT_CRITICAL();

    /*if the task running, it will goes into a while(1) loop,
      if the task return, it will go to the last two declarations*/
    /*EMOS_DBGSTR_VAL(stack->task);*/
    
    stack->task(stack->pData);
    stack->exit = 1;
    emosTaskDel(ptcb->osTCBPrio);

    return 0;
}

/*********************************************************************************************************
* WIN32 TASK - EMOS_SLEEP()
* Description: These functions are body of OS multitasking in WIN32.
* Arguments  : lpParameter   is a pointer to special paraleter of the task.
* Note(s)    : 1) Priorities of these tasks are very important.
**********************************************************************************************************/
void EMOS_SLEEP()
{
    Sleep(1);
}

/*********************************************************************************************************
* WIN32 TASK - EMOS_STOP()
* Description: These functions are body of OS multitasking in WIN32.
* Arguments  : lpParameter   is a pointer to special paraleter of the task.
* Note(s)    : 1) Priorities of these tasks are very important.
**********************************************************************************************************/
void EMOS_STOP()
{
    EMOS_ENTER_CRITICAL();
    ++gEmosTerminateTickW32;
    ++gEmosTerminateCtxSwW32;
    EMOS_TASK_SW();
    EMOS_EXIT_CRITICAL();
    Sleep(1000/EMOS_TICKS_PER_SEC);
}


/*
int EMOS_Printf(char *str, ...)
{
    int  ret;

    va_list marker;

    va_start( marker, str );

    EMOS_ENTER_CRITICAL();
    ret = vprintf( str, marker );
    EMOS_EXIT_CRITICAL();

    va_end( marker );

    return ret;
}
*/





/*MinGW changelist*/
/*
MinGW\include\basetsd.h line-56
typedef unsigned int UINT32;
typedef unsigned int  *PUINT32;

MinGW\include\winnt.h line-83
typedef unsigned long ULONG;
typedef unsigned long *PULONG;
*/


#endif

 /*
 * Please add "$" around "Id" and "Log".
 * $Id$
 * $Log$
 *
 */

 

⌨️ 快捷键说明

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