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

📄 timer.c

📁 samsung 9908DVD源代码,
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************************************
 * timer.c
 * coded by hspark@ce.cnu.ac.kr
 * date : 2001/06/23
 * modified by hjahn@ce.cnu.ac.kr
 * date : 2003/03/03
 **********************************************************************************/

#include "kernel\\mk_sys.h"
#include "kernel\\mk_task.h"
#include "kernel\\mk_timer.h"
#include "kernel\\mk_ddi.h"
#include "kernel\\mk_hisr.h"
#include "CalmRISC16\\irq.h"
#include "CalmRISC16\\CalmRISC16.h"

#ifdef _MK_TIMER


MK_TIMER *MK_pTimerActiveListHead;
MK_TIMER *MK_pTimerActiveListTail;
MK_TIMER *MK_pTimerListHead;
MK_TIMER *MK_pTimerListTail;
extern MK_TASK	*MK_pTaskDelayedListHead;

static STATUS MK_InsertToActiveTimerList(MK_TIMER *pTimer);
static STATUS MK_DeleteFromActiveTimerList(MK_TIMER *pTimer);
static VOID MK_TimerExpiration(VOID);

extern void 	IO_WData32(unsigned char *addr,unsigned int high,unsigned int low);
extern void    IO_WData32_EX(unsigned char *addr,unsigned int high,unsigned int low);
extern unsigned long IO_RData32_EX(unsigned char *addr);


ULONG	MK_Ticks;


VOID
MK_SystemTimerInitialize(VOID)
{
	MK_pTimerActiveListHead = MK_NULL;
	MK_pTimerActiveListTail = MK_NULL;
	MK_pTimerListHead = MK_NULL;
	MK_pTimerListTail = MK_NULL;
	MK_Ticks = 0;
}

STATUS
MK_CreateTimer(MK_TIMER *pTimer, CHAR *pName, MK_TIMER_FUNC_T Function, INT Arg1, CHAR *Arg2, 
			   ULONG InitialTime, ULONG RepeateTime, BOOLEAN Enable)
{
	INT	Flags;
    
    if( (pTimer == MK_NULL) || (pTimer->tm_Magic == MK_TIMER_MAGIC) )
		return MK_ERROR;
    	
	Flags = MK_InterruptDisable();		/* Critical Region */
	//MK_BlockCopy(pTimer->tm_pName, pName, MK_NAME_MAX-1);
	//pTimer->tm_pName[MK_NAME_MAX-1] = '\0';
	pTimer->tm_pName = pName;

	if(InitialTime > 0) {
		pTimer->tm_InitialTime = InitialTime;
		pTimer->tm_ExpireTime = InitialTime;
		pTimer->tm_Expirations = 0;
	}
	else {/*Just to report ERROR!!*/
		MK_Printf("WARNING!! I n i t i a l  T i m e  i s  0 !!\n");
		MK_InterruptRestore(Flags);
		return MK_ERROR;
	}
	
	if(RepeateTime > 0)
		pTimer->tm_RepeateTime = RepeateTime;
	else
		pTimer->tm_RepeateTime = 0;

	pTimer->tm_Arg1 = Arg1;
	pTimer->tm_Arg2 = Arg2;
	pTimer->tm_Function = Function;
	pTimer->tm_Status = MK_TIMER_DISABLE;
	pTimer->tm_pActivedNext = MK_NULL;
	pTimer->tm_pActivedPrev = MK_NULL;
	pTimer->tm_Magic = MK_TIMER_MAGIC;

	pTimer->tm_pNext = MK_NULL;
	pTimer->tm_pPrev = MK_NULL;
	if(MK_pTimerListHead == MK_NULL)
	{
		MK_pTimerListHead = pTimer;
		MK_pTimerListTail = pTimer;
	}
	else
	{
		pTimer->tm_pPrev = MK_pTimerListTail;
		MK_pTimerListTail->tm_pNext = pTimer;
		MK_pTimerListTail = pTimer;
	}

	if(Enable)
	{
		MK_InsertToActiveTimerList(pTimer);
	}

	MK_InterruptRestore(Flags);

	return MK_NO_ERROR;
}

static STATUS 
MK_InsertToActiveTimerList(MK_TIMER *pTimer)
{
	MK_TIMER *pNext;
	LONG	ExpireTime;
	INT		Flags;

//#if MK_DEBUG
	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return MK_RESOURCE_ERROR;
	}

	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_InsertToActiveTimerList() - Magic error!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
//#endif

	if(pTimer->tm_Status == MK_TIMER_ENABLE)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_InsertToActiveTimerList() - Active Timer is Activating one more!\n");
#endif
		return MK_ERROR;
	}

	Flags = MK_InterruptDisable();		/* Critical Region */

	ExpireTime = pTimer->tm_ExpireTime;
	if(ExpireTime <= 0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_InsertActivaeTimerList() - ExpireTime must be greate than zero!\n");
#endif		
		MK_InterruptRestore(Flags);
		return MK_ERROR;
	}

	pTimer->tm_pActivedNext = MK_NULL;
	pTimer->tm_pActivedPrev = MK_NULL;
	
	if(MK_pTimerActiveListHead == MK_NULL)
		MK_pTimerActiveListHead = MK_pTimerActiveListTail = pTimer;
	else {
		pNext = MK_pTimerActiveListHead;
		
		while(pNext) {
			if(ExpireTime < pNext->tm_ExpireTime)
				break;
			ExpireTime -= pNext->tm_ExpireTime;
			pNext = pNext->tm_pActivedNext;
		}
		
		if(pNext == MK_NULL) {
			pTimer->tm_pActivedPrev = MK_pTimerActiveListTail;
			MK_pTimerActiveListTail->tm_pActivedNext = pTimer;
			MK_pTimerActiveListTail = pTimer;
		}
		else if (pNext->tm_pActivedPrev == MK_NULL) {
			pNext->tm_pActivedPrev = pTimer;
			pTimer->tm_pActivedNext = pNext;
			MK_pTimerActiveListHead = pTimer;
			pNext->tm_ExpireTime -= ExpireTime;
		}
		else {
			pTimer->tm_pActivedPrev = pNext->tm_pActivedPrev;
			pTimer->tm_pActivedPrev->tm_pActivedNext = pTimer;
			pNext->tm_pActivedPrev = pTimer;
			pTimer->tm_pActivedNext = pNext;
			pNext->tm_ExpireTime -= ExpireTime;
		}
		pTimer->tm_ExpireTime = ExpireTime;
	}
	pTimer->tm_Status = MK_TIMER_ENABLE;

	MK_InterruptRestore(Flags);
	return MK_NO_ERROR;
}

static STATUS 
MK_DeleteFromActiveTimerList(MK_TIMER *pTimer)
{
	MK_TIMER *pNext;
	int Flags;

	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_DeleteFromActiveTimerList() - Magic error!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
	
	if(pTimer->tm_Status == MK_TIMER_DISABLE)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_DeleteFromActiveTimerList() - Deactive Timer is deactivating one more!\n");
#endif
		return MK_ERROR;
	}

	Flags = MK_InterruptDisable();			/* Critical Region */
	if(pTimer == MK_pTimerActiveListHead)
	{
		pNext = MK_pTimerActiveListHead->tm_pActivedNext;
		if(pNext == MK_NULL)
		{
			MK_pTimerActiveListHead = MK_NULL;
			MK_pTimerActiveListTail = MK_NULL;
		}
		else
		{
			pNext->tm_pActivedPrev = MK_NULL;
			pNext->tm_ExpireTime += pTimer->tm_ExpireTime;
			MK_pTimerActiveListHead = pNext;
		}
	}
	else
	{
		pNext = pTimer->tm_pActivedNext;
		if(pNext == MK_NULL)
		{
			pTimer->tm_pActivedPrev->tm_pActivedNext = MK_NULL;
			MK_pTimerActiveListTail = pTimer->tm_pActivedPrev;
		}
		else
		{
			pTimer->tm_pActivedPrev->tm_pActivedNext = pTimer->tm_pActivedNext;
			pTimer->tm_pActivedNext->tm_pActivedPrev = pTimer->tm_pActivedPrev;
			pTimer->tm_pActivedNext->tm_ExpireTime += pTimer->tm_ExpireTime;
		}
	}
	pTimer->tm_pActivedNext = MK_NULL;
	pTimer->tm_pActivedPrev = MK_NULL;

	pTimer->tm_Status = MK_TIMER_DISABLE;
	MK_InterruptRestore(Flags);

	return MK_NO_ERROR;
}

ULONG
MK_GetTimerRepeateTime(MK_TIMER *pTimer)
{
//#if MK_DEBUG
	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return 0;
	}
	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_GetTimerRepeateTime() - Magic error!\n");
#endif
		return 0;
	}
//#endif

	return pTimer->tm_RepeateTime;
}

ULONG
MK_SetTimerRepeateTime(MK_TIMER *pTimer, ULONG NewTime)
{
	ULONG	Time;
	INT		Flags;

//#if MK_DEBUG
	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_SetTimerRepeateTime() - Magic error!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
//#endif

	Flags = MK_InterruptDisable();

	Time = pTimer->tm_RepeateTime;
	pTimer->tm_RepeateTime = NewTime;

	MK_InterruptRestore( Flags );
	return Time;
}

BOOLEAN
MK_GetTimerStatus(MK_TIMER *pTimer)
{
//#if MK_DEBUG
	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_GetTimerStatus() - Magic error!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
//#endif

	return pTimer->tm_Status;

}

STATUS
MK_ControlTimer(MK_TIMER *pTimer, BOOLEAN Enable)
{
	INT Flags;

//#if MK_DEBUG
	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_ControlTimer() - Magic error!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
//#endif

	Flags = MK_InterruptDisable();

	if( (Enable == MK_TIMER_ENABLE) && (pTimer->tm_Status != MK_TIMER_ENABLE) )
	{
		if(pTimer->tm_Expirations)
			pTimer->tm_ExpireTime = pTimer->tm_RepeateTime;
		else
			pTimer->tm_ExpireTime = pTimer->tm_InitialTime;
		MK_InsertToActiveTimerList(pTimer);
	}
	else if ( (Enable == MK_TIMER_DISABLE) && (pTimer->tm_Status != MK_TIMER_DISABLE) )
	{
		MK_DeleteFromActiveTimerList(pTimer);
	}

	MK_InterruptRestore( Flags );

	return MK_NO_ERROR;
}

STATUS
MK_DeleteTimer(MK_TIMER *pTimer)
{
	INT Flags;

//#if MK_DEBUG
	if(pTimer == MK_NULL)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_ControlTimer() - Timer doesn't exist!\n");
#endif
		return MK_RESOURCE_ERROR;
	}
	if(pTimer->tm_Magic != MK_TIMER_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_DeleteTimer() - Magic error!\n");
#endif
		return MK_RESOURCE_ERROR;

⌨️ 快捷键说明

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