📄 timer.c
字号:
INT32U SysTime()
{
unsigned long t, flags;
ULONG i,jiffies;
save_flags(flags);
Enlock();
t = jiffies * 11932;
outb_p(0, 0x43);
i = inb_p(0x40);
i |= inb(0x40) << 8;
restore_flags(flags);
return(t - i);
}
void Delay(INT16U tick)
{
tick=tick;
}
/*
*********************************************************************************************************
* 获得一个可用的ID号
*********************************************************************************************************
*/
static INT8 _NewTimerId()
{
static UNSIGNED Next_ID;
UNSIGNED id; /* process id to return */
EnLock();
if ( (id=Next_ID--) <MAX_TIMER_NUM) {
if (TimerTable[id] == NULL){
UnLock();
return(id);
}
}
else if(TimerFreeList){
id=TimerFreeList->id;
TimerFreeList=TimerFreeList->next_timer;
UnLock();
return(id);
}
UnLock();
return(NO_TIMER_SPACE);
}
/*
*********************************************************************************************************
创建一个应用定时器函数
*********************************************************************************************************
*/
SIGNED CreateTimer(APP_TIMER *timer_ptr, void(*expiration_routine)(UNSIGNED))
{
SIGNED timer_id,err;
if (expiration_routine==NULL) return (ERR_INVALID_FUNCTION);
timer_id=_NewTimerId();
if(timer_id<0)
return (timer_id);
if(timer_ptr==NULL) return ERR_INVALID_TIMER;
else if(TimerTable[timer_ptr->id]==timer_ptr) return (timer_ptr->id);
TimerTable[timer_id]=timer_ptr;
timer_ptr->type_id=APPTIMER;
timer_ptr->id=timer_id;
timer_ptr->expiration_routine=expiration_routine;
return(timer_id);
}
/*
*********************************************************************************************************
删除一个应用定时器函数
*********************************************************************************************************
*/
STATUS DelTimer(SIGNED timer_id)
{
APP_TIMER *timer_ptr;
if (timer_id<0 ||timer_id >=MAX_TIMER_NUM) { /* Not allowed to delete idle task */
return (ERR_INVALID_TIMER);
}
timer_ptr=TimerTable[timer_id];
if (timer_ptr == NULL) { /* Not allowed to delete idle event */
return (ERR_INVALID_TIMER);
}
#if DEBUG
DebugLog(DELETE_TIMER,timer_id);//在日志中记录操作,时间等信息
#endif
EnLock();
if (timer_ptr->status==ENABLE)
StopTimer((TIMER_TCB*)timer_ptr);
timer_ptr->type_id=0;
timer_ptr->next_timer=TimerFreeList;
TimerFreeList=timer_ptr;
TimerTable[timer_id]=NULL;
UnLock();
return (OK);
}
/*
*********************************************************************************************************
复位一个特定的应用定时器函数,在复位之前,该定时器必须处于停止状态,复位后如果该
函数规定了自动激活允许标志,则定时器激活
*********************************************************************************************************
*/
STATUS ResetTimer (SIGNED timer_id, void (*expiration_routine)(UNSIGNED),
INT32U initial_time, UNSIGNED enable)
{
APP_TIMER *timer_ptr;
if (timer_id<0 ||timer_id >=MAX_TIMER_NUM) { /* Not allowed to delete idle task */
return (ERR_INVALID_TIMER);
}
if (timer_ptr==NULL) return(ERR_INVALID_TIMER);
if (expiration_routine==NULL) return (ERR_INVALID_FUNCTION);
if (enable!=ENABLE && enable!=DISABLE && enable!=FREE) return (ERR_INVALID_ENABLE);
#if DEBUG
DebugLog(RESET_TIMER,timer_id);//在日志中记录操作,时间等信息
#endif
timer_ptr=TimerTable[timer_id];
EnLock();
if (timer_ptr->status==ENABLE)
StopTimer((TIMER_TCB*)timer_ptr);
timer_ptr->expiration_time=initial_time;
timer_ptr->expiration_routine=expiration_routine;
timer_ptr->status=enable;
if(enable==ENABLE)
StartTimer((TIMER_TCB*)timer_ptr,initial_time);
UnLock();
return (OK);
}
STATUS AdjustTimer (SIGNED timer_id, INT32U new_time)
{
APP_TIMER *timer_ptr;
if (timer_id<0 ||timer_id >=MAX_TIMER_NUM) { /* Not allowed to delete idle task */
return (ERR_INVALID_TIMER);
}
#if DEBUG
DebugLog(ADJUST_TIMER,timer_id);//在日志中记录操作,时间等信息
#endif
timer_ptr=TimerTable[timer_id];
EnLock();
if (timer_ptr==NULL) return(ERR_INVALID_TIMER);
if (timer_ptr->status==ENABLE)
StopTimer((TIMER_TCB*)timer_ptr);
timer_ptr->expiration_time=new_time;
if(timer_ptr->status==ENABLE)
StartTimer((TIMER_TCB*)timer_ptr,new_time);
UnLock();
return (OK);
}
/*
*********************************************************************************************************
控制一个特定的应用定时器的运行
*********************************************************************************************************
*/
STATUS ControlTimer(SIGNED timer_id,UNSIGNED enable)
{
APP_TIMER *timer_ptr;
if (timer_id<0 ||timer_id >=MAX_TIMER_NUM) { /* Not allowed to delete idle task */
return (ERR_INVALID_TIMER);
}
if (enable!=ENABLE && enable!=DISABLE && enable!=FREE) return (ERR_INVALID_ENABLE);
#if DEBUG
DebugLog(CONTROLTIMER,timer_id);//在日志中记录操作,时间等信息
#endif
timer_ptr=TimerTable[timer_id];
EnLock();
if (timer_ptr == NULL) { /* Not allowed to delete idle event */
return (ERR_INVALID_TIMER);
}
if(timer_ptr->status & enable){
UnLock();
return (OK);
}
timer_ptr->status=enable;
if(enable==ENABLE)
StartTimer((TIMER_TCB*)timer_ptr,timer_ptr->expiration_time);
else
StopTimer((TIMER_TCB*)timer_ptr);
UnLock();
return (OK);
}
/*
*********************************************************************************************************
* 列出系统中存在的定时器信息
*********************************************************************************************************
*/
STATUS ListTimer(SIGNED timer_id,TIMER_DATA **timer_lst)
{
APP_TIMER *timer_ptr;
UNSIGNED i;
if (timer_id+1<0 ||timer_id >=MAX_TIMER_NUM) { /* Not allowed to delete idle task */
return (ERR_INVALID_TIMER);
}
if(timer_id>=0){
timer_ptr=TimerTable[timer_id];
EnLock();
if (timer_ptr == NULL) { /* Not allowed to delete idle task */
return (ERR_INVALID_TIMER);
}
(*timer_lst)->enable=timer_ptr->status;
(*timer_lst)->id= timer_ptr->expiration_id;
(*timer_lst)->initial_time=timer_ptr->expiration_time;
UnLock();
return (OK);
}
else{
for(i=0;i<MAX_TIMER_NUM;i++){
timer_ptr=TimerTable[timer_id];
EnLock();
if (timer_ptr) { /* Not allowed to delete idle task */
(*timer_lst)->enable=timer_ptr->status;
(*timer_lst)->id= timer_ptr->expiration_id;
(*timer_lst)->initial_time=timer_ptr->expiration_time;
timer_lst++;
}
UnLock();
}
return (OK);
}
}
/*
*********************************************************************************************************
* 定时器模块的初始化函数
*********************************************************************************************************
*/
void TimerInit(void)
{
UNSIGNED i;
for(i=0;i<MAX_TIMER_NUM;i++)
TimerTable[i]=NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -