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

📄 emos_core.c

📁 emos是一个新的类似于ucos的内核
💻 C
📖 第 1 页 / 共 4 页
字号:
*
* Arguments  : none
* Returns    : none
* Note       : OSStartHighRdy() MUST:
*              a) Call OSTaskSwHook() then,
*              b) Set  OSRunning to TRUE.
**********************************************************************************************************/
void emosStart (void)
{
    uint8 y;
    uint8 x;

    /*only run once from the main entrance activation*/
    if (gEmosRunning == EMOS_FALSE) 
    {
        /* Find highest priority's task priority number */
        y = gEmosUnMapTbl[gEmosRdyGrp];       
        x = gEmosUnMapTbl[gEmosRdyTbl[y]];
        gEmosPrioHighRdy = (uint8)((y << 3) + x);
        gEmosPrioCur     = gEmosPrioHighRdy;
        
         /* Point to highest priority task ready to run */
        gEmosTCBHighRdy  = gEmosTCBPrioTbl[gEmosPrioHighRdy]; 
        gEmosTCBCur      = gEmosTCBHighRdy;
        
        emosTaskSwHook();
       /* Execute target specific code to start task */
        emosStartHighRdy();                            
    }
}


/**********************************************************************************************************
* STATISTICS INITIALIZATION
* Description: This function is called by your application to establish CPU usage by first determining
*              how high a 32-bit counter would count to in 1 second if no other tasks were to execute
*              during that time.  CPU usage is then determined by a low priority task which keeps track
*              of this 32-bit counter every second but this time, with other tasks running.  CPU usage is
*              determined by:
*
*                                             OSIdleCtr
*                 CPU Usage (%) = 100 * (1 - ------------)
*                                            OSIdleCtrMax
* Arguments  : none
* Returns    : none
**********************************************************************************************************/
#if EMOS_TASK_STAT_EN
void emosStatInit (void)
{
	#ifndef EMOS_CPU_MINGW_MODE
    /* Synchronize with clock tick,but for MingGW, Not use Delay before StartHigh */
    emosTimeDly(2); 
    #endif
    
    EMOS_ENTER_CRITICAL();
    
   /* Clear idle counter */
    gEmosIdleCtr    = 0;                          
    EMOS_EXIT_CRITICAL();

   /* Determine MAX. idle counter value for 1 second */
   #ifndef EMOS_CPU_MINGW_MODE
   emosTimeDly(EMOS_TICKS_PER_SEC);                 
   #endif
   
    EMOS_ENTER_CRITICAL();
    /* Store maximum idle counter count in 1 second */
    gEmosIdleCtrMax = gEmosIdleCtr;
   
    /*gEmosIdleCtrMax = 880000;*/
    gEmosStatRdy    = EMOS_TRUE;
    EMOS_EXIT_CRITICAL();
}
#endif

/**********************************************************************************************************
* IDLE TASK
* Description: This task is internal to EMOS and executes whenever no other higher priority tasks
*              executes because they are waiting for event(s) to occur.
* Arguments  : none
* Returns    : none
*********************************************************************************************************/
void emosTaskIdle (void *pdata)
{
   /* Prevent compiler warning for not using 'pdata'*/
   pdata = pdata;                               
   for (;;)
   {
   	    //EMOS_Printf("Task Idle Running cnt=%d\r\n",(int)gEmosIdleCtr);
        EMOS_ENTER_CRITICAL();
        gEmosIdleCtr++;
        gEmosIdleTickCnt++;
        EMOS_EXIT_CRITICAL();

        /*should not add any delay to the idle task, because it is always running
        if(EMOS_TRUE == gEmosRunning)
        {
          emosTimeDly(100);
        }
        */
        
        /*emosDbgShow();*/ 
    }
}

#if EMOS_TASK_STAT_EN
void emosTaskStat (void *pdata)
{
    uint32   run;
    int8     usage;

   /* Prevent compiler warning for not using 'pdata'*/    
   pdata = pdata;                               
   while (gEmosStatRdy == EMOS_FALSE) 
   {
      /* Wait until statistic task is ready,*/
      emosTimeDly(2 * EMOS_TICKS_PER_SEC);           
   }
    
   for (;;)
   {
        EMOS_ENTER_CRITICAL();
       /* Obtain the of the idle counter for the past second */
        gEmosIdleCtrRun = gEmosIdleCtr;                
        run = gEmosIdleCtr;

       /* Reset the idle counter for the next second*/
        gEmosIdleCtr    = 0;                       
        EMOS_EXIT_CRITICAL();

       if (gEmosIdleCtrMax > 0)
       {
            usage = (int8)(100 - 100 * run / gEmosIdleCtrMax);
            if (usage > 100) 
            {
                gEmosCpuUsage = 100;
            }

           else if (usage < 0)
           {
                gEmosCpuUsage =   0;
           } 

           else
           {
                gEmosCpuUsage = usage;
           }
       }

       else
       {
            gEmosCpuUsage = 0;
       }

        /* Invoke user definable hook*/
        emosTaskStatHook();
        
        /*
        if(gEmosIdleCtrMax < gEmosIdleCtrRun)
        {	
        	gEmosIdleCtrMax = gEmosIdleCtrRun;
        }
        */
        
   	    EMOS_Printf("Task Stat Running cnt=%d max=%d cpu=%%%02d\r\n",(int)gEmosIdleCtrRun, (int)gEmosIdleCtrMax,(int)gEmosCpuUsage);

        /* Accumulate OSIdleCtr for the next second */             
        emosTimeDly(EMOS_TICKS_PER_SEC);             
    }
   
}
#endif


/**********************************************************************************************************
* INITIALIZE TCB
* Description: This function is internal to EMOS and is used to initialize a Task Control Block when
*              a task is created (see OSTaskCreate() and OSTaskCreateExt()).
* Arguments  : prio          is the priority of the task being created
*              ptos          is a pointer to the task's top-of-stack assuming that the CPU registers
*                            have been placed on the stack.  Note that the top-of-stack corresponds to a 
*                            'high' memory location is OS_STK_GROWTH is set to 1 and a 'low' memory
*                            location if OS_STK_GROWTH is set to 0.  Note that stack growth is CPU
*                            specific.
*              pbos          is a pointer to the bottom of stack.  A NULL pointer is passed if called by
*                            'OSTaskCreate()'.
*              id            is the task's ID (0..65535)
*              stk_size      is the size of the stack (in 'stack units').  If the stack units are INT8Us
*                            then, 'stk_size' contains the number of bytes for the stack.  If the stack
*                            units are INT32Us then, the stack contains '4 * stk_size' bytes.  The stack
*                            units are established by the #define constant OS_STK which is CPU
*                            specific.  'stk_size' is 0 if called by 'OSTaskCreate()'.
*              pext          is a pointer to a user supplied memory area that is used to extend the task
*                            control block.  This allows you to store the contents of floating-point
*                            registers, MMU registers or anything else you could find useful during a 
*                            context switch.  You can even assign a name to each task and store this name
*                            in this TCB extension.  A NULL pointer is passed if called by OSTaskCreate().
*              opt           options as passed to 'OSTaskCreateExt()' or, 
*                            0 if called from 'OSTaskCreate()'.
* Returns    : OS_NO_ERR         if the call was successful
*              OS_NO_MORE_TCB    if there are no more free TCBs to be allocated and thus, the task cannot
*                                be created.
* Note       : This function is INTERNAL to EMOS and your application should not call it.
**********************************************************************************************************/
uint8 emosTCBInit(uint8 prio, EMOS_STK *ptos, EMOS_STK *pbos, uint16 id, uint16 stk_size, void *pext, uint16 opt)
{
    EMOS_TCB_T* ptcb = NULL;
    
    EMOS_ENTER_CRITICAL();
    ptcb = gEmosTCBFreeList;  /* Get a free TCB from the free TCB list  */

    if (ptcb != (EMOS_TCB_T *)0) 
    {
        gEmosTCBFreeList = ptcb->osTCBNext;  /* Update pointer to free TCB list */
        EMOS_EXIT_CRITICAL();
        ptcb->osTCBStkPtr    = ptos;         /* Load Stack pointer in TCB  */
        ptcb->osTCBPrio      = (uint8)prio;  /* Load task priority into TCB */
        ptcb->osTCBStat      = EMOS_STAT_RDY;  /* Task is ready to run  */
        ptcb->osTCBDly       = 0;            /* Task is not delayed */

#if EMOS_TASK_CREATE_EXT_EN        
        ptcb->osTCBExtPtr    = pext;       /* Store pointer to TCB extension   */
        ptcb->osTCBStkSize   = stk_size;   /* Store stack size */
        ptcb->osTCBStkBottom = pbos;       /* Store pointer to bottom of stack */
        ptcb->osTCBOpt       = opt;        /* Store task options  */
        ptcb->osTCBId        = id;         /* Store task ID */
#else
        pext                 = pext;  /* Prevent compiler warning if not used  */
        stk_size             = stk_size;
        pbos                 = pbos;
        opt                  = opt;
        id                   = id;
#endif

#if EMOS_TASK_DEL_EN        
        ptcb->osTCBDelReq    = EMOS_NO_ERR;
#endif
        /* Pre-compute X, Y, BitX and BitY */
        ptcb->osTCBY         = prio >> 3;  
        ptcb->osTCBBitY      = gEmosMapTbl[ptcb->osTCBY];
        ptcb->osTCBX         = prio & 0x07;
        ptcb->osTCBBitX      = gEmosMapTbl[ptcb->osTCBX];

#if     EMOS_MBOX_EN || (EMOS_Q_EN && (EMOS_MAX_QS >= 2)) || EMOS_SEM_EN
        ptcb->osTCBEventPtr  = (EMOS_EVENT_T *)0; /* Task is not pending on an event */
#endif

#if     EMOS_MBOX_EN || (EMOS_Q_EN && (EMOS_MAX_QS >= 2))
        ptcb->osTCBMsg       = (void *)0;  /* No message received */
#endif

        EMOS_ENTER_CRITICAL();
        gEmosTCBPrioTbl[prio]   = ptcb;
        ptcb->osTCBNext      = gEmosTCBList; /* Link into TCB chain*/
        ptcb->osTCBPrev      = (EMOS_TCB_T *)0;
        if (gEmosTCBList != (EMOS_TCB_T *)0)
        {
            gEmosTCBList->osTCBPrev = ptcb;
        }

        /*TCBList point to the new allocated tcb*/
        gEmosTCBList               = ptcb;
        gEmosRdyGrp               |= ptcb->osTCBBitY; /* Make task ready to run */
        gEmosRdyTbl[ptcb->osTCBY] |= ptcb->osTCBBitX;
        EMOS_EXIT_CRITICAL();
        
        return (EMOS_NO_ERR);
    } 
    else 
    {
        EMOS_EXIT_CRITICAL();
        return (EMOS_NO_MORE_TCB);
    }
}

/**********************************************************************************************************
* PROCESS SYSTEM TICK
* Description: This function is used to signal to EMOS the occurrence of a 'system tick' (also known
*              as a 'clock tick').  This function should be called by the ticker ISR but, can also be
*              called by a high priority task.
* Arguments  : none
* Returns    : none
**********************************************************************************************************/
void emosTimeTick (void)
{
    EMOS_TCB_T* ptcb = NULL;

    #if EMOS_TASK_STAT_EN
    /*add for emos adjust the max idle cnt*/
    if(gEmosIdleCtrMax < gEmosIdleTickCnt * 100)
    {
    	gEmosIdleCtrMax = gEmosIdleTickCnt * 100;
    }
    #endif
    
    gEmosIdleTickCnt = 0;       
    emosTimeTickHook();     /* Call user definable hook */
    ptcb = gEmosTCBList;    /* Point at first TCB in TCB list */
    
    while (ptcb->osTCBPrio != EMOS_IDLE_PRIO) 
    {  
    	/* Go through all TCBs in TCB list*/
        EMOS_ENTER_CRITICAL();
        if (ptcb->osTCBDly != 0) 
        {   

        	/* Delayed or waiting for event with TO*/
        	/*modified by zenf at April 1th 2008, if osTCBDly equals to 0xFFFF, that means WAIT FOREVER,
        	  do not need to decrease the counters*/
        	if (ptcb->osTCBDly != EMOS_WAIT_FOREVER)
        	{
               if (--ptcb->osTCBDly == 0)
               {   
               	/* Decrement nbr of ticks to end of delay   */
                   if (!(ptcb->osTCBStat & EMOS_STAT_SUSPEND)) 

⌨️ 快捷键说明

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