原创 一个简单的任务管理器源码

2008-1-2 17:58 2961 8 8 分类: MCU/ 嵌入式

pdf


pdf


pdf


pdf


#ifndef _TIMER_H_
#define _TIMER_H_


#include "type.h"


#define TIMER0_PRESCALER 1024
#define MAX_TIMER_NO 16
#define SYSTEM_RHYTHM_MS 20//20ms


class CManagerTimer
{


private:
 UINT16 m_TimerCount[MAX_TIMER_NO];
 UINT16 m_TimerCountInit[MAX_TIMER_NO];
 UINT8 m_TimerEnable[MAX_TIMER_NO/8];
 UINT8 m_TimerIsrTaskPrio[MAX_TIMER_NO];
public:
 UINT8 m_TCNT0;


 CManagerTimer();
 UINT8 HardTimerInit(UINT8 ms);
 void HardTimerStart(void);
 void HardTimerClose(void);
 
 UINT8 SetTimer(UINT8 id,UINT8 prio,UINT32 ms);
 UINT8 KillTimer(UINT8 id);
 void TimerDecrease(void);
};



#endif


/************************************************************
 FileName: timer.cpp


 Author:胡贵   


 Version :V1.0     


 Date: 2008.1.2


 Description:
  system function for timer manager
  1.set timer
  2.kill timer
    
 History:


      <author>      <time>      <version >      <desc>



************************************************************/


#include "includes.h"
#include "global.h"


CManagerTimer::CManagerTimer()
{
 UINT8 i;


 for(i=0;i<MAX_TIMER_NO/8;i++)
 {
  m_TimerEnable=0;
 }


 HardTimerInit(SYSTEM_RHYTHM_MS);
}


UINT8 CManagerTimer::HardTimerInit(UINT8 ms)
{


 m_TCNT0=0XFF-(UINT8)(ms*1.0/(1000*(1.0/(SYSTEM_FREQUENCY_HZ*1.0/TIMER0_PRESCALER))));//85;//
 TCNT0=m_TCNT0;
 TCCR0|=0X07;//1024 PRESCALER
 return 1;
}



void CManagerTimer::HardTimerStart(void)
{
 TCCR0|=0X07;//1024 PRESCALER
 TIMSK|=0x01;//OVERFLOW INTERRUPT ENABLE
}


void CManagerTimer::HardTimerClose(void)
{
 TCCR0&=0xf8;
 TIMSK&=(~0x01);//OVERFLOW INTERRUPT DISABLE
 TIFR|=0x01;//CLEAR OV FLAG
}


UINT8 CManagerTimer::SetTimer(UINT8 id,UINT8 prio,UINT32 ms)
{
 if(id>=MAX_TIMER_NO)
 {
  return 0;
 }
 else
 {
  if(id>=64)
  {
   return 0;  
  }
  else
  {
   if((ms/SYSTEM_RHYTHM_MS)>65535)
   {
    return 0;
   }
   else
   {
    if(m_TimerEnable[id/8]&(0x01<<(id%8)))
    {
     return 0;
    }
    else
    {
     m_TimerCount[id]=(ms/SYSTEM_RHYTHM_MS);
     m_TimerCountInit[id]=m_TimerCount[id];
     m_TimerIsrTaskPrio[id]=prio;
     cli();
     m_TimerEnable[id/8]|=(0x01<<(id%8));
     sei();
     //SystemDebug("Timer set id is:%d     prio is:%d\n",(UINT16)id,(UINT16)prio);
     return 1;
    }
   }
  }
 }
}


UINT8 CManagerTimer::KillTimer(UINT8 id)
{
 if(id>=MAX_TIMER_NO)
 {
  return 0;
 }
 else
 {
  cli();
  m_TimerEnable[id/8]&=(~(0x01<<(id%8)));
  sei();
  return 1;
 }
}


void CManagerTimer::TimerDecrease(void)
{
 UINT8 i;
// SystemDebug("---\n");
 for(i=0;i<MAX_TIMER_NO;i++)
 {
  if(m_TimerEnable[i/8]&(0x01<<(i%8)))
  {
   if(!(--m_TimerCount))
   {
    m_TimerCount=m_TimerCountInit;
    ObjTaskManager.TaskRdy( m_TimerIsrTaskPrio);
    //WriteLog("PRIO:%d ",m_TimerIsrTaskPrio);
   }
  }
 }
}


 


SIGNAL(SIG_OVERFLOW0)
{
 TCNT0=TIMER0.m_TCNT0;
 PORTA=0XFF;
 TIMER0.TimerDecrease();
}


#ifndef _TASK_MANAGER_H_
#define _TASK_MANAGER_H_


#include "type.h"


class CManagerTask
{
//取值为8、16、24、32、40、48、56、64
#define MAX_TASK_NO 64
private:
 UINT8 m_TaskRdyGrp;
 UINT8 m_TaskRdyTbl[MAX_TASK_NO/8];
 void (*(m_pTask[MAX_TASK_NO]))(void);
public:
 CManagerTask();
 UINT8 TaskInit(void);
 void  TaskRun(void);
 UINT8 TaskCreat(void (*pTask)(void),UINT8 prio);
 void TaskRdy(UINT8 prio);
 void TaskSleep(UINT8 prio);
};



#endif


/************************************************************
 FileName: task_manager.cpp


 Author:胡贵   


 Version :V1.0     


 Date: 2008.1.2


 Description:
  some system functions for task manager.
  imitate the UCOS-II.So some of the code is cut from there.
  In this task manager:
  1.Assume that every task cost a very short time slice,
     no long time delay function called in the task.
     If a time delay function have to be called in this task,
     we just have to seperate the large task to small task which
     cost very short time.In that case,the TCB or PCB (this is necessarry in the OS)
     is not needed.After all,here the Task Manager writen by me is only a
     task manager,not a OS.In some samll(just SMALL) application,that's enough and simple
     and reduce the RAM cost.
  2.The task schedule  base on the priority of each task.
     The Task Manager select the highest priotity task which is ready,
     then run it,after runing done, make it sleep.It doesn't record the times that the
     task need to be run,it's just one time running ,until the next time when this task is ready.
  3.Time Tick:20ms
    


 History:


      <author>      <time>      <version >      <desc>



************************************************************/



#include "includes.h"
#include "global.h"


UINT8 const  TaskUnMapTbl[256] = {
    0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x00 to 0x0F                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x10 to 0x1F                             */
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x20 to 0x2F                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x30 to 0x3F                             */
    6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x40 to 0x4F                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x50 to 0x5F                             */
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x60 to 0x6F                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x70 to 0x7F                             */
    7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x80 to 0x8F                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0x90 to 0x9F                             */
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0xA0 to 0xAF                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0xB0 to 0xBF                             */
    6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0xC0 to 0xCF                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0xD0 to 0xDF                             */
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,       /* 0xE0 to 0xEF                             */
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0        /* 0xF0 to 0xFF                             */
};


UINT8 const  TaskMapTbl[8]   = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};


CManagerTask::CManagerTask()
{
 TaskInit();
}



UINT8 CManagerTask::TaskInit(void)
{
 UINT8 i;
 
 m_TaskRdyGrp=0;
 for(i=0;i<(MAX_TASK_NO/8);i++)
 {
  m_TaskRdyTbl=0;
  m_pTask=0;
 }


 TaskRdy(63);//idle task is ready ,so be sure a idle task is created first.
 return 1;
}


 


//y    = OSUnMapTbl[OSRdyGrp];
 
//x    = OSUnMapTbl[OSRdyTbl[y]];
 
//prio = (y << 3) + x;
 


void CManagerTask::TaskRun(void)
{
 UINT8 y,x,prio;
   
 while(1)
 {
  cli();
  y=TaskUnMapTbl[m_TaskRdyGrp];
  x=TaskUnMapTbl[m_TaskRdyTbl[y]];
  sei();
  prio=(y<<3)+x;
  (*m_pTask[prio])();
  if(prio!=63)
  {
   //PORTA=0XFF;
   TaskSleep(prio);
  }
  else
  {
   //PORTA=0X00;
  }
 }
}


UINT8 CManagerTask::TaskCreat(void (*pTask)(void),UINT8 prio)
{
 if(m_pTask[prio]==0)
 {
  m_pTask[prio]=pTask;
  SystemDebug("Task create ok!---prio is:%d\n",(UINT16)prio);
  return 1;
 }
 else
 {
  SystemDebug("Task create failed!---prio is:%d\n",(UINT16)prio);
  return 0;
 }
}


 


void CManagerTask::TaskRdy(UINT8 prio)
{
// SystemDebug("Task ready!---prio is:%d\n",(UINT16)prio);
 cli();
  m_TaskRdyGrp|=TaskMapTbl[prio>>3];
 m_TaskRdyTbl[prio>>3]|=TaskMapTbl[prio&0x07];
 sei();
}



void CManagerTask::TaskSleep(UINT8 prio)
{
 cli();
 if ((m_TaskRdyTbl[prio >> 3] &= ~TaskMapTbl[prio & 0x07]) == 0)
  m_TaskRdyGrp &= ~TaskMapTbl[prio >> 3];
 sei();
}



 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
8
关闭 站长推荐上一条 /3 下一条