📄 timetisr.c
字号:
/*
miniTOS V0.1.3 1998-2004 (c) 林良水 (Lin LS)
miniTOS是一个开放源码的软件,授权LGPL,但开发人员不保证本软件的可靠性,
以及对您的损失负任何责任。
www.minitos.com
本文实现miniTOS定时器实现。
create by Lin LS ,2005.6
*/
#include <stdio.h>
#include "minitosdef.h"
#include "extdefine.h"
#include "TimerDef.h"
APP_TIMER * AddLinkToAppTimerLastPos(APP_TIMER *head,APP_TIMER *pAppTimer);
//System Timer Task ISR define
TISR SysTimerTisr;
//stack for System Timer TISR
char SysTimerTisrStk[1024];
//System Timer struct link head
APP_TIMER *ptrAppTimerHead;
//任务级的定时中断服务
//定时服务程序和Tisr使用同一个堆栈,即定时服务程序作为Tisr的一个子程序被调用
void Tisr_SysTimerEntry(void)
{
APP_TIMER *pAppTimer;
void (*timer_server)(uint32);
//判断所有定时器是否过期
//如果过期执行相应定时服务
//定时服务程序和Tisr使用同一个堆栈,即定时服务程序作为Tisr的一个子程序被调用
pAppTimer = ptrAppTimerHead;
while(pAppTimer!=NULL)
{ //循环所有定时器,对每个RemainCounter减一
if(pAppTimer->enable)
{
pAppTimer->RemainTime--;
}else
{
//取下一个定时器
pAppTimer=pAppTimer->ptrNextAppTimer;
continue;
}
if(pAppTimer->RemainTime==0)
{//定时时间到
timer_server = pAppTimer -> TimerServer;
if(timer_server!=NULL)
{
(*(timer_server)) (pAppTimer ->TimerID);
}
if(pAppTimer->count==0xffffffff)
{//定时器永远运行
//重新置记数
pAppTimer->RemainTime=pAppTimer->InitTime;
}else
{
if( --(pAppTimer->count) )
{
//重新置记数
pAppTimer->RemainTime=pAppTimer->InitTime;
}else
{//count==0
//关闭定时器
pAppTimer->enable=MO_DISABLE;
}
}
}
//取下一个定时器
pAppTimer=pAppTimer->ptrNextAppTimer;
}
}
void SysTimerInit(void)
{
//定时器链表初始化
ptrAppTimerHead=NULL;
//建立定时器TISR
CreateTISR(&SysTimerTisr,Tisr_SysTimerEntry,SysTimerTisrStk+1024,2);
}
int CreateTimer(APP_TIMER *timer,int TimerID,void(*time_server)(uint32),
int InitTime, int count, int enable)
{
//定时器的序号
timer->TimerID=TimerID;
//定时服务程序入口
timer->TimerServer=time_server;
//定时周期时间
timer->InitTime=InitTime;
//定时次数,0或0xffffffff为永远运行
timer->count =count;
//先假定为关闭状态,才能加到链表,防止定时中断到引起意外情况
timer->enable = MO_DISABLE;
//把定时器加到定时服务链表中
AddLinkToAppTimerLastPos(ptrAppTimerHead,timer);
if(enable==MO_ENABLE)
{
timer->enable = MO_ENABLE;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////
//定时器的链表处理子程序
//bug????以后需要把链表统一处理
////////////////////////////////////////////////////////////////////////////
/*******************************************************************
**Function:
**Input: head--
**Output:ptrLastPos
**return: 链表中共有几项,0--空表,>0项数
********************************************************************/
APP_TIMER * AppTimer_FindLastPos(APP_TIMER *head)
{
APP_TIMER *ptrTmp,*ptrTmpOld;
if(head==NULL)
return NULL; //空表
ptrTmp=head;
ptrTmpOld=head;
while(ptrTmp!=NULL)
{
ptrTmpOld=ptrTmp;
ptrTmp=ptrTmp->ptrNextAppTimer;
}
//现在ptrTmpOld指向链表最后一个位置
return ptrTmpOld;
}
/*******************************************************************
**Function:
**Input: head--
rp--要找的指针
**Output: ptrPos-指向rp的前一个项的指针
如图: ptrPos->A->B
rp->B
**return: 链表中第几项,-1--空表,0,第一项,1--第二项...
********************************************************************/
APP_TIMER * FindAppTimerPos(APP_TIMER *head,APP_TIMER *rp,int *cnt)
{
APP_TIMER *ptrTmp,*ptrTmpOld;
*cnt=-1;
if(head==NULL)
return NULL; //空表,return NULL,cnt=-1
*cnt=0;
ptrTmp=head;
ptrTmpOld=head;
while(ptrTmp!=rp)
{
(*cnt)+=1;
ptrTmpOld=ptrTmp;
ptrTmp=ptrTmp->ptrNextAppTimer;
}
return ptrTmpOld;
}
/*******************************************************************
**Function:
**Input: head--
pProc--要加的进程指针
**Output:
none
**return:
********************************************************************/
APP_TIMER * AddLinkToAppTimerLastPos(APP_TIMER *head,APP_TIMER *pAppTimer)
{
APP_TIMER *ptrTmp;
ptrTmp=AppTimer_FindLastPos(head);
if(ptrTmp==NULL)
return NULL;
//加入到尾部
ptrTmp->ptrNextAppTimer=pAppTimer;
pAppTimer->ptrNextAppTimer=NULL;
return pAppTimer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -