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

📄 emos_cpu.c

📁 emos是一个新的类似于ucos的内核
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *
 * (c) Copyright 2001,2008, EMB system, All Rights Reserved.
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF EMB SYSTEM, INC.
 * The copyright notice above does not evidence any actual or intended
 * publication of such source code. 
 *
 *  Subsystem:   EMOS 
 *  File:        emos_cpu.c
 *  Author:      zenf zhao
 *  Description: this file include different CPU specific features to be supported
 *
 ****************************************************************************/

#include "emos_core.h" 
#include "emos_cpu.h"

uint8  gEmosTickDosCtr=0;      





#ifdef EMOS_CPU_X86_BCC_MODE
#include "..\cpux86\os_cpu_X86.c"
#endif






/*keep this section code as the last section of emos_cpu.c*/
#ifdef EMOS_CPU_MINGW_MODE 
#include <windows.h>

/*#include "..\cpum86\emos_cpumgw.c"*/
#define    ALLOW_CS_RECURSION    /* allow recursion of critical sections*/
#define    SET_AFFINITY_MASK     /* must be used for miltiprocessor systems*/
#define    EMOS_CPU_TRACE        /* allow print trace messages*/
#define    WIN_MM_TICK           /* Enabling WIN_MM_TICK will cause the uC/OS-II port to use the high resolution Multimedia  */
#define    WIN_MM_MIN_RES (1) 

DWORD WINAPI emosCtxSwW32( LPVOID lpParameter );
DWORD WINAPI emosTickW32 ( LPVOID lpParameter );
DWORD WINAPI emosTaskW32 ( LPVOID lpParameter );

uint32              gEmosTerminateTickW32     = 0;
uint32              gEmosTerminateCtxSwW32    = 0;
uint32              gEmosTerminateTickCtrlW32 = 0;
HANDLE              gEmosTick32Handle;
HANDLE              gEmosCtxSwW32Event;
HANDLE              gEmosCtxSwW32Handle;

#ifdef WIN_MM_TICK
HANDLE              gEmosTickEventHandle;
DWORD               gEmosTickTimer;
TIMECAPS            gEmosTimeCap;
#endif

CRITICAL_SECTION    gEmosCriticalSection;
HANDLE              gEmosSemaphore;

EMOS_EMU_STK*       SS_SP;

#ifdef ALLOW_CS_RECURSION
    DWORD           gThreadID = 0;
    int             gRecursion = 0;
#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.
**********************************************************************************************************/
void emosInitHookBegin(void)
{
#ifdef USE_CRITICAL_SECTION
    InitializeCriticalSection(&gEmosCriticalSection);
#else
    gEmosSemaphore = CreateSemaphore( NULL, 1, 1, NULL );
#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.
**********************************************************************************************************/
void emosInitHookEnd(void) {}

/*********************************************************************************************************
* EMOS_INIT_CRITICAL()
* Initialize code necessary for WIN32 implementation 
* of ENTER and EXIT CRITICAL routines.
**********************************************************************************************************/
void EMOS_INIT_CRITICAL()
{
#ifdef USE_CRITICAL_SECTION
    InitializeCriticalSection(&gEmosCriticalSection);
#endif
}

/********************************************************************************************************
* EMOS_ENTER_CRITICAL()
* Defines the beginning of a critical section of code.
*********************************************************************************************************/
void EMOS_ENTER_CRITICAL()
{
#ifdef USE_CRITICAL_SECTION
    EnterCriticalSection(&gEmosCriticalSection);
#else

#ifdef ALLOW_CS_RECURSION

    if( WaitForSingleObject(gEmosSemaphore, 0 ) == WAIT_TIMEOUT )
    {
        if( GetCurrentThreadId() != gThreadID )
        {
        	WaitForSingleObject( gEmosSemaphore, INFINITE );
        }
    }
    
    gThreadID = GetCurrentThreadId();
    ++gRecursion;

#else
    WaitForSingleObject(gEmosSemaphore, INFINITE );
#endif
#endif
}

/*********************************************************************************************************
* EMOS_EXIT_CRITICAL()
* Defines the end of a critical section of code.
**********************************************************************************************************/

void EMOS_EXIT_CRITICAL()
{
#ifdef USE_CRITICAL_SECTION
    LeaveCriticalSection(&gEmosCriticalSection);
#else
#ifdef ALLOW_CS_RECURSION

    if( gRecursion > 0 ) 
    {
        if(--gRecursion == 0 )
        {
            gThreadID = 0;
            ReleaseSemaphore(gEmosSemaphore, 1, NULL );
        }
    }
    else 
    {
#ifdef EMOS_CPU_TRACE
    EMOS_Printf("Error: EMOS_EXIT_CRITICAL\\n");
#endif
    }

#else
    ReleaseSemaphore(gEmosSemaphore, 1, NULL );
#endif
#endif
}

/*********************************************************************************************************
* INITIALIZE A TASK'S STACK
* Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
*              stack frame of the task being created.  This function is highly processor specific.
* Arguments  : task          is a pointer to the task code
*              pdata         is a pointer to a user supplied data area that will be passed to the task
*                            when the task first executes.
*              ptos          is a pointer to the top of stack.  It is assumed that 'ptos' points to
*                            a 'free' entry on the task stack.  If EMOS_STK_GROWTH is set to 1 then 
*                            'ptos' will contain the HIGHEST valid address of the stack.  Similarly, if
*                            EMOS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
*                            of the stack.
*              opt           specifies options that can be used to alter the behavior of OSTaskStkInit().
*                            (see uCEMOS_II.H for EMOS_TASK_OPT_???).
* Returns    : Always returns the location of the new top-of-stack' once the processor registers have
*              been placed on the stack in the proper order.
* Note(s)    : Interrupts are enabled when your task starts executing. You can change this by setting the
*              PSW to 0x0002 instead.  In this case, interrupts would be disabled upon task startup.  The
*              application code would be responsible for enabling interrupts at the beginning of the task
*              code.  You will need to modify OSTaskIdle() and OSTaskStat() so that they enable 
*              interrupts.  Failure to do this will make your system crash!
**********************************************************************************************************/

void *emosTaskStkInit (void (*task)(void *pd), void *pdata, void *ptos, uint16 opt)
{
    EMOS_EMU_STK* stk = NULL;

    stk             = (EMOS_EMU_STK *)((char*)ptos-sizeof(EMOS_EMU_STK));   /* Load stack pointer*/
    stk->pData      = pdata;
    stk->opt        = opt;
    stk->task       = task;
    stk->handle     = NULL;
    stk->id         = 0;
    stk->exit       = 0;

    return ((void *)stk);
}


#if EMOS_CPU_HOOKS_EN
/*********************************************************************************************************
* 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.
**********************************************************************************************************/
void emosTCBInitHook(EMOS_TCB_T *ptcb)
{
    EMOS_EMU_STK  *stack;
    
    stack = (EMOS_EMU_STK*) ptcb->osTCBStkPtr;
    stack->handle = CreateThread( NULL, 0, emosTaskW32, ptcb, CREATE_SUSPENDED, &stack->id );
    
    #ifdef SET_AFFINITY_MASK
    if( SetThreadAffinityMask( stack->handle, 1 ) == 0 ) 
	{	
        #ifdef EMOS_CPU_TRACE
        EMOS_Printf("Error: SetThreadAffinityMask\n");
        #endif
    }
    #endif
}

void emosTaskCreateHook (EMOS_TCB_T *ptcb)
{
	/*TaskCreateHook call by EMOS Core Source TaskCreate(),
	 so it invoke the TCBInitHook to create the thread*/
	emosTCBInitHook(ptcb);
}

/*********************************************************************************************************
* 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.
**********************************************************************************************************/

void emosTaskDelHook (EMOS_TCB_T *ptcb)
{
    EMOS_EMU_STK* stack;
    
    stack = (EMOS_EMU_STK*) ptcb->osTCBStkPtr;

    if(!(stack->exit))
    {
        SuspendThread(stack->handle);
        CloseHandle(stack->handle);
    }
}

/*********************************************************************************************************
* TASK SWITCH HOOK
* Description: This function is called when a task switch is performed.  This allows you to perform other
*              operations during a context switch.
* Arguments  : none
* Note(s)    : 1) Interrupts are disabled during this call.
*              2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
*                 will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the 
*                 task being switched out (i.e. the preempted task).
**********************************************************************************************************/

void emosTaskSwHook (void)
{
}

/*********************************************************************************************************
* STATISTIC TASK HOOK
* Description: This function is called every second by uC/OS-II's statistics task.  This allows your 
*              application to add functionality to the statistics task.
* Arguments  : none
**********************************************************************************************************/
void emosTaskStatHook (void)
{
}

/*********************************************************************************************************
*                                               TICK HOOK
* Description: This function is called every tick.
* Arguments  : none
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
**********************************************************************************************************/
void emosTimeTickHook (void)
{
}

/*********************************************************************************************************
* TASK IDLE HOOK

⌨️ 快捷键说明

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