📄 timer.c
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// TIMER.C
//
// Timer Support
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>
// Timer
typedef struct _timer {
uint Type; // Set to HTYPE_TIMER
struct _timer *pNext; // Pointer to next timer
void (*pHandler)( uint ); // Message Handler
uint Msg; // Message to send on trigger
uint Trigger; // Trigger val (in half secs)
uint Period; // Trigger val (in half secs)
} TTIMER;
static TTIMER *ptFirst = 0;
static uint MasterTicks = 0;
static void TimerListInsert( TTIMER *pt );
static void TimerListRemove( TTIMER *pt );
//--------------------------------------------------------------------
// TimerNew()
//
// Creates a task owned timer
//--------------------------------------------------------------------
HANDLE TimerNew( void (*pHandler)(uint), uint HSCount, uint Msg )
{
TTIMER *pt;
// Timers should all be alloced up front - If the alloc fails,
// we're pretty much toast. However, we won't lock ...
if( !(pt = mmAlloc(sizeof(TTIMER))) )
{
DbgPrintf(DBG_ERROR,"TimerNew: OOM");
ExecLowResource();
return( 0 );
}
// Initialize type
pt->Type = HTYPE_TIMER;
// Initialize "program"
pt->pHandler = pHandler;
pt->Msg = Msg;
pt->Period = HSCount;
// Insert into list
TimerListInsert(pt);
return( (HANDLE)pt );
}
//--------------------------------------------------------------------
// TimerFree()
//
// Destroys a task owned timer
//--------------------------------------------------------------------
void TimerFree( HANDLE h )
{
TTIMER *pt = (TTIMER *)h;
#ifdef _STRONG_CHECKING
if( pt->Type != HTYPE_TIMER )
{
DbgPrintf(DBG_ERROR,"TimerFree: HTYPE %04x",pt->Type);
return;
}
#endif
// Remove from list
TimerListRemove( pt );
// Kill type for debug
pt->Type = 0;
mmFree( h );
}
//--------------------------------------------------------------------
// TimerHSTick()
//
// Services all timers
//--------------------------------------------------------------------
void TimerHSTick()
{
TTIMER *pt;
// Bump the master count
MasterTicks++;
// The timeout list is pre-sorted by time. Continue triggering
// timers until we get ONE that isn't ready.
while( (pt=ptFirst) && pt->Trigger == MasterTicks )
{
// Go to next timer
ptFirst = pt->pNext;
// Post Task Message
pt->pHandler( pt->Msg );
// Put TIMER back in list
TimerListInsert(pt);
}
}
//--------------------------------------------------------------------
// TimerListInsert()
//
//--------------------------------------------------------------------
static void TimerListInsert( TTIMER *pt )
{
TTIMER *ptTmp;
pt->Trigger = pt->Period + MasterTicks;
// Check the easy case - being first
if( !ptFirst
|| (ptFirst->Trigger-MasterTicks) >= (pt->Trigger-MasterTicks) )
{
// What we point to next
pt->pNext = ptFirst;
// Before us...pointing to us
ptFirst = pt;
}
else
{
// Find an entry we expire AFTER
ptTmp = ptFirst;
while( ptTmp->pNext && (ptTmp->pNext->Trigger-MasterTicks)
< (pt->Trigger-MasterTicks) )
ptTmp = ptTmp->pNext;
// What we point to next (can be NULL)
pt->pNext = ptTmp->pNext;
// Before us...pointing to us (can be overwiting a NULL)
ptTmp->pNext = pt;
}
}
//--------------------------------------------------------------------
// TimerListRemove()
//
//--------------------------------------------------------------------
static void TimerListRemove( TTIMER *pt )
{
TTIMER *ptTmp;
// Check to see if we're the head of the list
if( pt == ptFirst )
ptFirst = pt->pNext;
else
{
// Look for us
ptTmp = ptFirst;
while( ptTmp && ptTmp->pNext != pt )
ptTmp = ptTmp->pNext;
// Patch entry which points to us (if any)
if( ptTmp )
ptTmp->pNext = pt->pNext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -